How to add "Run SLA calculation" to a script

ashamankun
Giga Contributor

I'm modifying the end time on a lot of old tickets. This is a one time deal to fix an issue.   The below script works for that but the SLA calculation is not updating. I found the Run SLA Calculation action that is doing it in the normal closing process but am not sure how to add it to my script or even if that is the best way of doing this. Any suggestions would be appreciated.

*Total coding newbie here so don't judge my script to harshly   . I'm only grabbing one ticket just for testing purposes, but will change when ready to run for all tickets I need to change.

updateQuick();

function updateQuick(){

    var quick = new GlideRecord('incident');

      quick.addQuery ('sys_id' , 'ticket I want');

    quick.query();

    while (quick.next()){

            var quicksysid = quick.sys_id;

            gs.log("incident id:" + quicksysid);

         

            var updateSLA = new GlideRecord('task_sla');

            updateSLA.addQuery('task', quicksysid);

            updateSLA.query();

              while (updateSLA.next()){

                   

                            var starttime = updateSLA.start_time;                                  

                            var endtime = new GlideDateTime(starttime);

                      endtime.addSeconds(900);

                      gs.log("End Time:" + " " + endtime);

                          updateSLA.end_time = endtime;

                                 

                            updateSLA.update();

                       

                            SLACalculatorNG.calculateSLA(current, /* skipUpdate */ false, current.end_time);   < ========== Where I would like to update the SLA calculations but not sure how to use this.

                                 

                    }

            gs.log("Found: " + quicksysid);

     

}

      gs.log("Done");

}

1 ACCEPTED SOLUTION

Awesome, thank you Tyler. That almost worked. I had to make one minor modification. I had to add updatesla.end_time after false.




So the working version is SLACalculatorNG.calculateSLA(updateSLA, /* skipUpdate */ false, updateSLA.end_time);


View solution in original post

3 REPLIES 3

Tyler Hoge - Gl
Tera Guru

here is just the SLA section of the code.



var sla = new GlideRecord('task_sla');


sla.addQuery('task', 'task sys_id here');


sla.query();


while(sla.next()){


  SLACalculatorNG.calculateSLA(sla, /* skipUpdate */ false);


}



You were close, just change SLACalculatorNG.calculateSLA(current, /* skipUpdate */ false, current.end_time); to SLACalculatorNG.calculateSLA(updateSLA, /* skipUpdate */ false);


In addition to what Tyler said above, I would also suggest removing the first query entirely. If you're querying the incident based on sys_id, then you already have the sys_id you need for the task_sla table query.


Awesome, thank you Tyler. That almost worked. I had to make one minor modification. I had to add updatesla.end_time after false.




So the working version is SLACalculatorNG.calculateSLA(updateSLA, /* skipUpdate */ false, updateSLA.end_time);