Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

SLAs Not Stopping After Work request is closed

Afrin12
Tera Contributor

SLAs Not Stopping After Work Request Auto-Closure

 

Afrin12_0-1741622838552.png

Scheduled job runs every day to close the work request automatically,
Script:

function ProcWorkOrdAuto() {
var wrk = new GlideRecord('u_work_order');
wrk.addEncodedQuery('state=9^ORstate=3^NQstate=4^sys_updated_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()');
wrk.query();

while (wrk.next()) {
var sla = new GlideRecord('task_sla');
sla.addEncodedQuery("task=" + wrk.sys_id + "^sla=a95fe6e9dbfd10905dc552625b96190e^active=true");
sla.query();

while (sla.next()) {
var seconds = sla.u_idb_business_duration.getGlideObject().getNumericValue() / 1000;
if (seconds >= 259200) { // 3 days
wrk.state = "4";
wrk.work_notes = "Work Request automatically closed by system.";
wrk.active = false;
wrk.closed_at = gs.nowDateTime();
wrk.update();
}

if (wrk.state == '4') {
SLACalculatorNG.calculateSLA(wrk, false);
}
}
}
}


But Sla is in progress

Afrin12_2-1741623054185.png

 



Afrin12_1-1741623025966.png

Any suggestions on what might be missing? Thanks in Advance.

 

 

12 REPLIES 12

start and pause conditions are working properly. I will try again.
Thank you very much for your quick response.

SyedMahemoH
Tera Expert
Hi @Afrin12,
If you want the SLA to stop when the state is "Closed," consider:
Removing the second condition (State is not Completed) if it conflicts with your first condition and do some changes on your script in Schedule job.
function ProcWorkOrdAuto() {
    var wrk = new GlideRecord('u_work_order');
    wrk.addEncodedQuery('state=9^ORstate=3^NQstate=4^sys_updated_on>=javascript&colon;gs.beginningOfThisWeek()^sys_updated_on<=javascript&colon;gs.endOfThisWeek()');
    wrk.query();

    while (wrk.next()) {
        var sla = new GlideRecord('task_sla');
        sla.addEncodedQuery("task=" + wrk.sys_id + "^sla=a95fe6e9dbfd10905dc552625b96190e^active=true");
        sla.query();

        while (sla.next()) {
            var seconds = sla.u_idb_business_duration.getGlideObject().getNumericValue() / 1000;
            if (seconds >= 259200) { // 3 days
                wrk.state = 4; // Assuming state is an integer
                wrk.work_notes = "Work Request automatically closed by system.";
                wrk.active = false;
                wrk.closed_at = gs.nowDateTime();
                wrk.update();
                break; // Exit the inner loop after closing the work request
            }
        }

        // Call SLA calculation only if the work request is closed
        if (wrk.state == 4) {
            SLACalculatorNG.calculateSLA(wrk, false);
        }
    }
}
If my response helped you, please accept the solution and mark it as helpful.
Thank You!

Thank you , I will try this way