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.

Issue in closing RITM for parallel Tasks

Michael51
Tera Guru

Hello all,

I have an requirement to close RITM when all the Respective Tasks get closed , So here is our workflow behaves interms of creating Task

1. Workflow Will create 2 Tasks , Initially Single Task gets created post completing the initial task another task gets opened 

 

In order to close RITM I have written BR on Sc_task table and it is on after update 

 

 

 

    /* var count = 0;
    var tasks = new GlideRecord('sc_task');
    tasks.addQuery('request_item', current.request_item);
    //tasks.addQuery('active',true);//added
    tasks.query();
    //if(!task.next()){//added
    var taskcount = tasks.getRowCount();
    while (tasks.next()) {
        gs.info("passing into 1if loop");
        if (tasks.state == 3 || tasks.state == 4 || tasks.state == 7) {
            count++;
        }
    }
    gs.info('taskcount' + '' + count + 'yesbank');
    if (count == taskcount) {
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('sys_id', current.request_item);
        gr.query();
        if (gr.next()) {

            gr.state = 3;

            if (current.state == 3 || current.state == 7)
                gr.state = 3;
            else if (current.state == 4)
                gr.state = 4;
            gr.update();
        }
    }

 

 

 

 

here the issues is it is working for Single task and as soon as initial task gets closed RITM is getting closed but it s not waiting for next Task to complete , here I want to validate all the Tasks to get completed and proceed with RITM Closure 

Can all help with your inputs on this 

( I want to acheive this BR instead of adding Runscript in Workflow Since we want to work globally (we have lot of workflows)

@Ankur Bawiskar  

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael51 

you will have after update BR on sc_task

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

Script: Enhance it as per your requirement

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

	// Add your code here
	var gr = new GlideRecord('sc_task');
	gr.addQuery('request_item', current.request_item); 
	gr.addQuery('active', true);
	gr.query();
	if(!gr.next()){
		var ritm = current.request_item.getRefRecord(); 
		ritm.state = 3;
		ritm.update();
	}

})(current, previous);

 

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

I have used this script already and it is working for initial task (like as soon as first task gets closed RITM is getting closed , ) 

Hello Ankur,

I have updated my code according to the requirement, now issue is even afer closing all Tasks RITM is getting strucked at pending status(attached screenshot)

 

1st BR - this aborts RITM closure when there are any active tasks on it

 if (gs.getSession().isInteractive())
        var sysid = current.sys_id;
    var gr = new GlideRecord("sc_task");
    gr.addActiveQuery();
    gr.addQuery('request_item', sysid);
    gr.query();
    if (gr.next()) {
        gs.addInfoMessage("Please close all the tasks before closing the request");
        current.setAbortAction(true);
    }

 

2nd BR- on Sc_task table(after update) to close RITM when all tasks get closed tasks

var gr = new GlideRecord('sc_task');

    gr.addQuery('request_item', current.request_item);

    gr.query();

    var totalTask = gr.getRowCount();

    var gr1 = new GlideRecord('sc_task');

    gr1.addQuery('request_item', current.request_item);

    gr1.addEncodedQuery('stateIN3,4,7'); //checks for closed complete,closed skipped,closed incomplete

    gr1.query();

    var closedTaskCount = gr1.getRowCount();

    if (totalTask == closedTaskCount) {

        var req = new GlideRecord('sc_req_item');
        req.addQuery('sys_id', current.request_item);
        req.query();
        if (req.next()) {



            if (current.state == 3 )
                req.state = 3;
            else if (current.state == 4||curent.state==7 )
                req.state = 4;
            current.work_notes = 'testing'; // setting worknores as this field is mandatory on closed skipped and closed incomplete
            req.update();

@Ankur Bawiskar 

Michael51_0-1665720972313.png

even after approving approval the stage is getting strucked in waiting for approval by which request is getting strucked in pending