How to close all the RITM, when all REQTASKs are closed, using Business rule, for a Catalog Form?

AbdurRahmanSnow
Giga Guru

Good evening.!
I have a Catalog Form where the Flow designer is not working properly, as it has lot of Create REQTASK conditions, because of which, sometimes, the RITM remains open, even after the REQTASKs get closed. I don't want to touch the Flow designer, as it is very critical and uses automation.

So, how can we do this using Business rule? Please give the code.

Will the below BR code work for a particular Catalog Form.

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

try{
    var sctask = new GlideRecord('sc_req_item');
    sctask.addQuery('number', current.request_item.getDisplayValue());
    sctask.query();
    if(sctask.next())
        {
            if(sctask.cat_item.getDisplayValue() == 'Hardware Disposal Request')
                {
                    sctask.state = current.state;
                    sctask.update();
                }
    }
    }catch(ex){
        var message = ex.message;
        gs.error("Error in Hardware Disposal Request Form:"+ message);
    }

})(current, previous);



1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

1 RITM can have multiple sc_task

You can have after update BR on sc_task

Condition: State Changes to Closed Complete or Closed incomplete or Closes Skipped

Script:

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

1 RITM can have multiple sc_task

You can have after update BR on sc_task

Condition: State Changes to Closed Complete or Closed incomplete or Closes Skipped

Script:

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur. Thanks a lot for this code.

But the code should work only for a particular Catalog item, and not others. Will it work ?

@AbdurRahmanSnow 

in the BR condition add this along with your existing one

current.request_item.cat_item.name == 'Your Item Name Here'

If my response helped please mark it correct and close the thread so that it benefits future readers.

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