RITM States

Community Alums
Not applicable

Hi All,

 

In RITM the following business rule setting the state upon closure of first task but the requirement is it should wait for all tasks to close

 

Tasks are getting generated one after other (Not all at a time).

Suppose there are 3 tasks on the workflow

1) If all 3 tasks are closed complete then RITM state should be "closed complete"

2) If one of the 3 tasks is closed complete then RITM state should be "closed incomplete"

3) If all tasks are closed skipped then RITM state should be "closed skipped" 

 

Business Rule

Table - sc_task

Condition - After update if state is one of Closed Complete, Closed Incomplete, Closed Skipped

 

Script -

(function executeRule(current, previous /*null when async*/) {

var grSCTask = new GlideRecord('sc_task');
grSCTask.addQuery('request_item', current.getValue('request_item'));
grSCTask.addActiveQuery(); // added to check for active tasks
grSCTask.query();
if (!grSCTask.hasNext()) // if there still are active SCtasks connected to this RITM, there is no need to set any state on the parent RITM yet
{
var closeCompleteCount = 0;
var closeSkippedCount = 0;
var closedIncompleteCount = 0;
var closedOtherCount = 0;

grSCTask = new GlideRecord('sc_task');
grSCTask.addQuery('request_item', current.getValue('request_item'));
grSCTask.query();
gs.info ("Line 17 : " + grSCTask);
while (grSCTask.next()) {
if (grSCTask.getValue('state') == 3) {
closeCompleteCount++;
}
else if (grSCTask.getValue('state') == 4){
closedIncompleteCount++;
}
else if (grSCTask.getValue('state') == 7){
closeSkippedCount++;
}
else {
closedOtherCount++;
}
}

var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.getValue('request_item'))){
if (closedIncompleteCount > 0) {
ritm.setValue('state', 4); // ritm is closed incomplete
ritm.update();
}
else if (closeSkippedCount > 0 && closeCompleteCount == 0 && closedIncompleteCount == 0) {
ritm.setValue('state', 7); // ritm is closed skipped
ritm.update();
}
else if (closeCompleteCount > 0 && closeSkippedCount == 0 && closedIncompleteCount == 0){
ritm.setValue('state', 3); // ritm is closed complete
ritm.update();
}

else {
gs.info('Some other combination of closed happened, not setting RITM state');
gs.info('Closed other count: ' + closedOtherCount);
}
}
}

})(current, previous);

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

You should do this in the actual workflow as it will be easier, work more reliably, and be easier for you or someone else to maintain as your requirements change.  First add a Condition to each task for each of the three possible results Closed Complete (activity.result==3), Closed Incomplete (activity.result==4), and Closed Skipped (activity.result==7).  Delete the Always condition.  You will also then have a Set Value activity to set the State to Closed Complete, one to set the State to Closed Incomplete, and one to set the State to Closed Skipped.  Coming out of each result of each task you will have a Run Script activity, and one Run Script activity at the beginning of the workflow.  In this first Run Script, declare 3 workflow variables and set them to 0, so like

workflow.scratchpad.cc = '0';
workflow.scratchpad.ci = '0';
workflow.scratchpad.cs = '0';

The Run Script coming out of each Condition on each task will increment the corresponding workflow variable.  I think these are always stored as strings, so the Run Script for Closed Complete would look like this:

workflow.scratchpad.cc = parseInt(workflow.scratchpad.cc) + 1;

After the last Catalog Task, the 3 Run Script activities would all lead into an If activity, the script of which would test if workflow.scratchpad.cc == '3', with the Yes path leading to the Closed Complete Set Values activity, and the No path leading to another If activity, the script of which would test if workflow.scratchpad.cs == '3', with the Yes path leading to the Closed Skipped Set Values activity, and the No path leading to the Closed Incomplete Set Values activity since that is the only other possible result - or whatever fits your logic, but you get the idea.