Comparing different values of the same attribute e.g. identifying the elapsed time between the last two assignment updates

Version 5

    Scenario

    In this scenario, we want to find out how much time has elapsed between the last two assignments of an Incident.


    How to

    This can be achieved with a calculation. In this calculation we are using a "count"  to make the values identifiable and a "loop"  to go through each value. Finally, the two returned values are compared and the elapsed time is calculated.
    More information on how to write and troubleshoot calculations can be found here Calculations


    Solution

    You have to create a new calculated attribute inside your business object and add the calculation bellow. In this case the attribute is created on "Incident", and "Creation Date" from "Assignment" is selected as the target attribute. We are using an "After read" calculation so a dependency is not required.


    import System
    static def GetAttributeValue(Incident):
        if Incident.Assignments.Count > 1: //checks if there are more than one values in the collection
            count = 1 // creates the count
            LastAssignment = Incident.Assignments.Count
            SecondToLast = LastAssignment - 1
            LastAssignmentDate = DateTime.UtcNow
            SecondToLastAssignmentDate = DateTime.UtcNow
            for assignment in Incident.Assignments: //creates the for loop
                if count == SecondToLast:
                    SecondToLastAssignmentDate = assignment.CreationDate
                if count == LastAssignment:
                    LastAssignmentDate = assignment.CreationDate
                count = count + 1
            TimePeriod = LastAssignmentDate - SecondToLastAssignmentDate //compares the values
    
        else:
            TimePeriod = DateTime.UtcNow - DateTime.UtcNow //returns zeroes if there are not enough values to compare
        return String.Format("{0} Day(s), {1} Hour(s), {2} Minute(s), {3} Second(s)", Math.Floor(TimePeriod.TotalDays),TimePeriod.Hours, TimePeriod.Minutes, TimePeriod.Seconds)
    
    
    
    
    


    Result

    You can then enter the new calculated attribute on your window to display the results.

    Assignment Elapsed Time.PNG