Scheduled job script

GBS
Tera Contributor

I want to modify the script below, I want to update the state only when the existing sc task state is not closed complete or closed incomplete or closed skipped when the result ==2 and I want to update the state when the existing sc task state is not closed incomplete or closed skipped when the result ==3. Can anyone help me with the modified script

function updateSCTask(sys_id, result) {
    var gr = new GlideRecord("sc_task");
    if (gr.get(sys_id)) {
        if (result == "2" && gr.getValue("state") != 8){
            gr.setValue("state", "8");
            gr.setValue("u_on_hold_reason", 3);
            gr.setValue("follow_up", gs.daysAgo(-7));
        }
        if (result == "3" && gr.getValue("state") != 3)
            gr.setValue("state", "3");
        gr.update();
    }
}
1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @GBS 

Try this

function updateSCTask(sys_id, result) {
    var gr = new GlideRecord("sc_task");
    if (gr.get(sys_id)) {
        // When result is 2, update the state to 8 if the current state is not closed states (8, 7, or 6)
        if (result == "2" && gr.getValue("state") != 8 && gr.getValue("state") != 7 && gr.getValue("state") != 6) {
            gr.setValue("state", "8"); // Set state to Closed Complete (8)
            gr.setValue("u_on_hold_reason", 3); // Set the on-hold reason if applicable
            gr.setValue("follow_up", gs.daysAgo(-7)); // Set follow-up date
        }
        
        // When result is 3, update the state to 3 if the current state is not closed states (7 or 6)
        if (result == "3" && gr.getValue("state") != 7 && gr.getValue("state") != 6) {
            gr.setValue("state", "3"); // Set state to In Progress (3)
        }
        
        gr.update(); // Save the record after updates
    }
}

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

6 REPLIES 6

@GBS 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@GBS 

Can you check my below comment?

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