fix script to close multiple sctasks in ServiceNow

Ralton Stewart
Tera Guru

is anyone able to help me with a fix script to close multiple sctasks in ServiceNow (Utah)

1 ACCEPTED SOLUTION

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Ralton Stewart,

 

It really depends on which filter you want to apply, but here's the snippet you probably can change/enhance it later:

 

 

 

var gr = new GlideRecord('sc_task');

// Edit the filter below to target the specific records you want
gr.addQuery('state', '!=', 'Closed Complete'); // Example: Get all tasks that are not 'Closed Complete'
// Add more filter conditions as needed

gr.query();

while(gr.next()) {
    gr.state = 'Closed Complete'; // Set the state to 'Closed Complete'
    gr.update();
}

gs.addInfoMessage(gr.getRowCount() + ' sc_task records have been closed.');

 

Script Breakdown:

 

  1. We create a new GlideRecord object for the sc_task table.
  2. We added a query to filter the records. In the example above, we're getting all tasks that are not in the 'Closed Complete' state. You can edit this filter to target the specific records you want.
  3. We then loop through each record and update the state to 'Closed Complete'.
  4. Finally, we display a message indicating how many records were closed.

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯

 

View solution in original post

1 REPLY 1

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Ralton Stewart,

 

It really depends on which filter you want to apply, but here's the snippet you probably can change/enhance it later:

 

 

 

var gr = new GlideRecord('sc_task');

// Edit the filter below to target the specific records you want
gr.addQuery('state', '!=', 'Closed Complete'); // Example: Get all tasks that are not 'Closed Complete'
// Add more filter conditions as needed

gr.query();

while(gr.next()) {
    gr.state = 'Closed Complete'; // Set the state to 'Closed Complete'
    gr.update();
}

gs.addInfoMessage(gr.getRowCount() + ' sc_task records have been closed.');

 

Script Breakdown:

 

  1. We create a new GlideRecord object for the sc_task table.
  2. We added a query to filter the records. In the example above, we're getting all tasks that are not in the 'Closed Complete' state. You can edit this filter to target the specific records you want.
  3. We then loop through each record and update the state to 'Closed Complete'.
  4. Finally, we display a message indicating how many records were closed.

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯