The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Close many catalog tasks via a bulk operation.

greenbean
Mega Contributor

My client has a 110 service catalog tasks open pending other work. One task is related to one requested item. The client needs to close complete all 110 catalog tasks without manually updating them and additionally adding a generic message into 'Additional comments' from the requested item followed by closing the task which will automatically close the requested item. Would would be the best approach to doing this

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please create on-demand scheduled job; run as system administrator

You need to identify the catalog tasks

Go to table sc_task; apply the required query and use that in encoded query

Sample script below

1) Ensure you give proper query

2) ensure you run it for 5 records first; then confirm and then comment the setLimit() line of code

3) ensure you give proper state value for closure; I have given 3 meaning close complete

4) ensure you give proper message in comments field of task

5) please verify this by running in dev environment first before running in production

updateTasks();

function updateTasks(){

try{

var rec = new GlideRecord('sc_task');
rec.addEncodedQuery('<yourQueryHere>');
rec.setLimit(5); // remove this once you test for 5 records
rec.query();
while(rec.next()){

rec.state = 3; // close complete
rec.comments = 'This task has been closed';
rec.setWorkflow(false); // to avoid triggering any Business rule on catalog task
rec.update();

}

}
catch(ex){
gs.info('Exception is: ' + ex);
}


}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

@greenbean 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

I am glad to hear the comment has helped.

Saying this, I will appreciate if you close this thread by marking Answer as Correct & 👍Helpful so that it does not appear in unanswered list.

Have a great rest of the day

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

mdavis933
Mega Expert

So on the script you are turning off business rules... how you get also closed the RITM/REQ ? I understood the setWorkflow() will disable the execution of all business rules on the given table or any related table.

I think I came with something similar time ago and I had to do 2 scripts ( one for sc_task and other for sc_req_item) in order to avoid the emails getting out.