Update the state of sc_task

Angie15
Tera Contributor

I am doing some clean up, I need to close sc_tasks depending on some conditions with following script which is not updating the record. 

 

var task  = new GlideRecord('sc_task');

task.addEncodedQuery('addEncodedQueryhere');

task.query();

while (task.next()) {

task.state = '3'; //close complete

task.setWorkflow('false');

task.update();

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

What is the encoded query you are using?

 

Can you confirm you are adding a proper query like 'active=true' or 'number=SCTASK12345' and not just 'addEncodedQueryhere'

 

Also in the script you posted you are missing the closing } after task.update();

-Anurag

Aniket Chavan
Tera Sage
Tera Sage

Hello @Angie15 ,

Please give a try to the script below and see how it works for you.

// Set up the GlideRecord for the 'sc_task' table
var task = new GlideRecord('sc_task');

// Add your encoded query to filter the records you want to update
task.addEncodedQuery('addEncodedQueryhere');

// Execute the query
task.query();

// Loop through the result set
while (task.next()) {
    // Update the state to '3' (close complete)
    task.state = '3';

    // Disable workflow for this update
    task.setWorkflow(false);

    // Attempt to update the record
    if (task.update() == 'ERROR') {
        // Log an error message if the update fails
        gs.error('Error updating sc_task record: ' + task.getValue('number'));
    } else {
        // Log a success message if the update is successful
        gs.info('Updated sc_task record: ' + task.getValue('number'));
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket