how to add breach time on task_sla table based on schedule and start time

shweta14
Tera Contributor

Hi Community,

I have requirement to add breach time on task_sla table based on schedule- on SLA, Duration and start time.

I am triggering(creating )SLA from Flow designer but not calculating breach time.

How to Achieve This?

using below script but not helpful :-

var tz = gs.getSession().getTimeZone();
gs.info("tz<<<<<"+tz);
var currentDate = new GlideDateTime();
gs.info("currentDate<<<<<"+currentDate);
currentDate.setTZ(tz);
var days = 5;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * 5);
gs.info("dur<<<<<"+dur);
var sch = current.schedule;
var schedule = new GlideSchedule(sch);
gs.info("schedule<<<<<"+schedule);
var DayCal = schedule.add(currentDate, dur, '');
gs.info("DayCal<<<<<"+DayCal);
current.u_pending_date_time = DayCal;
current.update();

 

Thank you!

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

HI @shweta14 ,
I trust you are doing great.

  1. Create a new business rule that runs when a task SLA is created. This rule will execute the necessary script to calculate the breach time.

  2. Navigate to "Business Rules" in ServiceNow and create a new record with the following details:

    • Name: Calculate Task SLA Breach Time
    • Table: task_sla
    • When to run: Before
    • Insert: true
    • Update: true
  3. In the script section of the business rule, write the following code:

(function executeRule(current, previous) {
    var tz = gs.getSession().getTimeZone();
    var currentDate = new GlideDateTime();
    currentDate.setTZ(tz);
    var days = 5;
    var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
    var sch = current.schedule;
    var schedule = new GlideSchedule(sch);
    var breachTime = schedule.add(currentDate, dur, '');
    current.u_pending_date_time = breachTime;
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi