This example demonstrates how to use a scheduled calculation to move a process from one status to another. Specifically, it demonstrates how to move an Incident that has been Resolved for 40 hours to the Closed status. You can modify it so that it works in other modules, or to move between different statuses. For example, you could modify the calculation to return Calls that have been at the status With Customer for a certain time.
There are three stages to this calculation:
The first stage is to set up a calculation that tests to see if the Incident is ready to be closed. In stage 2 we will configure this to be calculated every night, but first we need to create a check box to store our calculation result.
import System static def GetAttributeValue(Incident): Flag = 'False' if Incident.Status.Name == 'Resolved': Resolution = Incident.Resolutions.Latest() TimeSinceResolved = Incident.GetBusinessTime(Resolution.CreationDate, DateTime.UtcNow) if TimeSinceResolved.TotalHours > 40: if DateTime.Today.DayOfWeek != DayOfWeek.Saturday and DateTime.Today.DayOfWeek != DayOfWeek.Sunday: Flag = 'True' return Flag
The first two lines have no indents. The line Flag = 'False' and the first line starting if have single indents. The lines from Resolution through to the second if have two indents. The line with the third if has three indents, and the line below it (Flag = 'True') has four indents. The final line (return Flag) has a single indent.
Initially, the flag is set to False, and then the calculation tests to see if the Incident is at the status Resolved. If the Incident is resolved, the calculation moves to the next section of the calculation.
Resolution = Incident.Resolutions.Latest() uses the collection function Latest() to return the Incident Resolution that has the latest creation date. We can then use Resolution.CreationDate in the next line of the calculation to give us the creation date of the latest resolution.
TimeSinceResolved = Incident.GetBusinessTime(Resolution.CreationDate, DateTime.UtcNow) uses the GetBusinessTime Object Function to determine the difference in business hours between the creation date of the latest resolution (Resolution.CreationDate), and the current time in UTC (the DateTime function DateTime.UtcNow) based on the calendar used by the Incident’s response level. (Most DateTimes in the database are stored in UTC.)
If this value is greater than 40 (1 working week = 5 x 8 = 40 hours), then the calculation tests that the day of the week is not equal to (!=) Saturday and not equal to Sunday. If this test is passed, it sets the flag to True.
The final line of the calculation returns the value of the flag as the value for the AutoClose attribute.
You can change this calculation to move a process from statuses other than Resolved by changing the status and collection that are used. For example, by using the With Customer status and collection, you could return processes to the analyst that have been with a customer for a certain period.
The next stage is to schedule the calculation to run. The schedule needs a query of incidents to test, so we also need to create this.
The scheduled calculation that we have just created sets a flag on the Incident to True, but doesn't actually close the Incident. To do that we need to schedule a bulk action to close any Incidents that have the flag set to True. First, we need to create a query to select the correct Incidents.
You can use this technique for any action, not just Close. However, make sure that the processes selected by the query have the relevant action available at that status.