Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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,

Thanks for your help, in that case where I should stop my workflow, Shall I remove set values and directly attach it to End?

find_real_file.png

Hi,

yes each catalog task can be directly attached to End Activity

regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Thank you so much Ankur! for sharing this.

Community Alums
Not applicable

Hi Ankur,

 

Sorry for I didn't check execution properly and I replied it is working. I am trying to understand why this business rule is not getting executed, I did not change anything in the script. I am keeping it for all RITMs

find_real_file.png

Business Rule -

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

    // Add your code here

    var closeCompleteCount = 0;
    var closeSkippedCount = 0;
    var closedIncompleteCount = 0;


    var ritm = current.request_item.getRefRecord();

    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item); 
    gr.query();
    var totalCount = gr.getRowCount();
    while(gr.next()){
        if(gr.state == 3)
            closeCompleteCount++;
        if(gr.state == 7)
            closeSkippedCount++;
        if(gr.state == 4)
            closedIncompleteCount++;
    }

    if(closeCompleteCount == totalCount)
        ritm.state = 3;

    else if(closeSkippedCount == totalCount)
        ritm.state = 7;

    else if(closedIncompleteCount >= 1)
        ritm.state = 4;

    ritm.update();


})(current, previous);

Workflow

find_real_file.png

Hi,

Did you add gs.info() in the business rule and verify the issue?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader