Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

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