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

kunal20
Kilo Guru

Hi Tejas,

 

Instead of using if in the second block use else if, use this for reference.

var grTask = GlideRecord('sc_task');
        var completed = 0;
        var skipped = 0;
        var count;
        grTask.addQuery('request_item','7242843e9711111096559300f153af19');
        grTask.query();

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

        }
        if(count == completed){
            gs.log('complete');
        }
        elseif(count == skipped){
            gs.log('skip');
        }
        else
            gs.log('incomplete');
 
please mark it as helpful
   
 

Ian Mildon
Tera Guru

Not sure on the rest of your Workflow but we have all the states defined at the Catalog Task activity. And depending on the State value set at the SCTASK, the Workflow then updates the RITM.

find_real_file.png

Community Alums
Not applicable

Hi Ian,

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" 

Thanks,

Tejas

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Tejas,

why not have after update BR on sc_task

Condition: State [IS ONE OF] Closed Complete/Closed Skipped/Closed Incomplete

I think you are saying closed incomplete

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

Regards
Ankur

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

@Tejas

Something like this

find_real_file.png

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

	// Add your code here

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


	var ritm = curent.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);

Regards
Ankur

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