Need to change the status of the sc task using background script.

Gayathree Seeth
Tera Expert

We have used a mandatory variable in the catalog item form and the user missed to fill the mandatory form and the form has been got submitted.

Now the team working on the SCTASK is not able to change the state of task since the user failed to give mandatory field.

 

How can i change the state of the SCTASK to pending. Can i use the background script or BR.

PLS suggest.

Thannks,

 

1 ACCEPTED SOLUTION

Paul Curwen
Giga Sage

Give this a go, you can generate your encoded query needed from the sc_task list filter, and alter the State number to whatever you require. 

 

 

// 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'));
    }
}

 

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

View solution in original post

3 REPLIES 3

PrashantLearnIT
Giga Sage

HI @GSeeth 

 

Why run the script to fill that mandatory field?

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Hi Prashant,

If i can able to write BG script for filling that mandatory fields that would be fine.

 

Paul Curwen
Giga Sage

Give this a go, you can generate your encoded query needed from the sc_task list filter, and alter the State number to whatever you require. 

 

 

// 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'));
    }
}

 

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul