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

Community Alums
Not applicable

Hi All,

 

I have been trying hard to set the RITM state to "closed skipped" if all tasks associated to that RITM are "closed skipped"

I am using following script in If activity of workflow but it is not working, please help!

 

find_real_file.png

 

Script in If activity

answer = allCompleted();

    function allCompleted() {
        var grTask = GlideRecord('sc_task');
        var completed = 0;
        var skipped = 0;
        var count;
        grTask.addQuery('request_item', current.sys_id);
        grTask.query();

        count = grTask.getRowCount();
        while (grTask.next()) {
            if (grTask.getValue('state') === '3') {
                completed++;
            }
            if (grTask.getValue('state') === '7') {
                skipped++;
            }

        }
        if (count == completed) { 
            return complete;
        }
        if (count == skipped) { 
            return skip;
        }
        else 
            return incomplete;
    }

and if activity results configured like below

find_real_file.pngThe results we are looking for -

We are actually checking for multiple tasks,

Suppose we have 3 tasks in the workflow, the below states we want on RITM

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" 

 

Please let me know where I am doing mistake

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Ankur,

 

Following script worked for me.

(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();
        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);

Thank you for your help as well

View solution in original post

15 REPLIES 15

Vasantharajan N
Giga Sage
Giga Sage

This behavior is because you set your stage to Completed in the workflow activity. 


Thanks & Regards,
Vasanth

Abhijit4
Mega Sage

What output are you getting for this activity when testing for record?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Community Alums
Not applicable

Hi Abhijit,

 

I tried with below script, it is working for closed complete and closed incomplete but we have to add closed skipped condition where we can add that one, please guide us.

 

Script

answer = allCompleted();

function allCompleted() {
    var grTask = GlideRecord('sc_task');
    var completed = 0;
    var count;
    grTask.addQuery('request_item', current.sys_id);
    grTask.query();

    count = grTask.getRowCount(); //checking the number of tasks on the RITM
    while (grTask.next()) {
        if (grTask.getValue('state') === '3') {
            completed++;
        }
    }
    return count === completed ? 'yes' : 'no'; // "yes" for closed complete and "no" for closed incomplete, here we want to add for "closed skipped", please suggest. (currently if all the tasks on the RITM are closed skipped it is setting state of RITM to "closed incomplete" but we want RITM state as "closed skipped")

}

We are trying to add skipped in "If" activity, PFB the screenshot

find_real_file.png

 

Community Alums
Not applicable

Hi Abhijit,

 

It is not setting any state on RITM, means RITM state is remaining as it is as "Open" upon closing of all tasks