Change SCTASK to cancelled state using Background script

JP6
Tera Contributor

Hi all, 

 

I have requirement to manually close a SCTask that did not get cancelled when the RITM was "cancelled". Please assist with Background script that I can run to update the SCTask to cancelled state .

1 ACCEPTED SOLUTION

You can use below logic:

var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addQuery('request_item','sys_id_of_ritm');
scTaskGr.addActiveQuery();
scTaskGr.query();
while(scTaskGr.next()){
scTaskGr.state = 4;  // 4 is Closed Incomplete
scTaskGr.setWorkflow(false);
scTaskGr.update();
}

 Replace sys_id of RITM in above script.

Make sure to put some logs and check which task is getting updated. Comment scTaskGr.update() while validating above script.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Anil Lande
Kilo Patron

Hi,

Can you please share more details?

How many task? is there any specific task to be cancelled?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

You can use below logic:

var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addQuery('request_item','sys_id_of_ritm');
scTaskGr.addActiveQuery();
scTaskGr.query();
while(scTaskGr.next()){
scTaskGr.state = 4;  // 4 is Closed Incomplete
scTaskGr.setWorkflow(false);
scTaskGr.update();
}

 Replace sys_id of RITM in above script.

Make sure to put some logs and check which task is getting updated. Comment scTaskGr.update() while validating above script.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @JP6 ,

Please use the below script to close the task using background script :

 

var kids = new GlideRecord('sc_task');
kids.addQuery('parent', 'your RITM sys_id');
kids.query();
while (kids.next()) {
kids.state = 4;

kids.setWorkflow(false); // will not run the business after making the changes

kids.autoSysFields(false); // will not update the sys fields on the table

kids.update();
}

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Prince Arora
Tera Sage
Tera Sage

@JP6 

 

You need to apply filters on "sc_task", as mentioned below screenshot:

 

PRINCE_ARORA_0-1680071805290.png

 

and apply on the scripts by copying the query mentioned below:

 

var tasks = new GlideRecord("sc_task");
tasks.addEncodedQuery("request_item.state=4^stateIN1,8");
task.query();
while(tasks.next()){
tasks.state = 4;
tasks.setWorkflow(false);
tasks.update();
}

 

Please let me know if you need more information!

 

Please mark this answer as correct/helpful based on the impact 🙂