ServiceNow script for calculating the due date of an incident based on its priority.

dmarpaka
Tera Contributor
I tried writing a before insert BR and calculate the due date by adding created date and based on the priority of the incident. I am getting a compilation error, can someone help me achieve this
 
(function executeRule(current, previous /*null when async*/ ) {

    // Check if the incident is being created
    if (current.operation() == 'insert') {

        // Get the incident created date
        var createdDate = new GlideDateTime(current.getValue('sys_created_on'));

        // Get the priority of the incident
        var priority = current.getValue('priority');

        // Calculate SLA duration based on priority
        var slaDuration;
        if (priority == '1') {
            slaDuration = new GlideDuration('0 01:00:00'); // Priority 1 gets 1 hour SLA time
        } else if (priority == '2') {
            slaDuration = new GlideDuration('0 08:00:00'); // Priority 2 gets 8 hours SLA time
        } else if (priority == '3') {
            slaDuration = new GlideDuration('1 00:00:00'); // Priority 3 gets 1 day SLA time
        } else if (priority == '4') {
            slaDuration = new GlideDuration('2 00:00:00'); // Priority 4 gets 2 days SLA time
        }

        // Add SLA duration to created date

        var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
        var dueDate = schedule.add(createdDate, slaDuration);
        current.u_due_date = dueDate;
    }

})(current, previous);
1 ACCEPTED SOLUTION

Ryan Duce
Tera Guru

Using a script for this rather than out-of-box functionality is not recommended. I would recommend you configure your SLAs as follows:

 

TableStart conditionUser-specified durationSchedule
incidentPriority = 11 hour<your schedule>
incidentPriority = 28 hours<your schedule>
incidentPriority = 31 day<your schedule>
incidentPriority = 42 days <your schedule>

 

Then, you should build a report on the out-of-box incident_sla table, which joins the incident table with the task_sla table and includes the "Breach time" field from the generated task_sla record, which is automatically populated with the due date you're looking for.

 

Benefits of this approach

  • No custom code which must be tested after each upgrade
  • No hard-coded sys-ids or durations - easy to reconfigure the timings of the SLAs
  • No custom fields
  • Additional SLAs can easily be added at a later date - the solution is far more scalable

Please mark as correct or helpful if this assisted you!

View solution in original post

8 REPLIES 8

dmarpaka
Tera Contributor

Thank you, let me check what you have suggested and will update you if it is working as expected.

Tai Vu
Kilo Patron
Kilo Patron

Hi @dmarpaka 

It appears that your script works well from my side. Can you provide more details about the error?

 

Cheers,

Tai Vu

dmarpaka
Tera Contributor

I am trying it as a POC for my org. Once it works will update you more about it.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @dmarpaka 

 I am not a coder, but cant we set the SLA end time in due date?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************