SLA

SohamJ
Tera Contributor

Assignment: In Resolution SLA make Duration type Dynamic, i.e., if category is software, then duration will be 2 hours and if category is hardware, then duration will be 3 hours.

 For the above assignment I had written the below code for creating custom 'Relative Duration'. Please suggest if there are any changes in the code. Also please do let me know all the configurations to be made on the SLA form.

(function () {
  var startDateMs = calculator.startDateTime.getNumericValue();
  var dueDate;
  var offsetMs = 0;

  // Determine time offset based on category
  var category = '';
  var tableName = current.getTableName();
  if (tableName) {
    var baseTableName = GlideDBObjectManager.getAbsoluteBase(tableName);
    if (baseTableName == "task")
      category = current.category.toString();
    else if (baseTableName == "task_sla")
      category = current.task.category.toString();
  }

  if (category == 'software') {
    offsetMs = 2 * 60 * 60 * 1000; // 2 hours in ms
  } else if (category == 'hardware') {
    offsetMs = 3 * 60 * 60 * 1000; // 3 hours in ms
  } else {
    offsetMs = 1000; // fallback: 1 second
  }

  // Add offset to start time
  dueDate = new GlideDateTime(calculator.startDateTime);
  dueDate.add(offsetMs);

  // If schedule exists and dueDate is outside schedule, push to next valid time
  if (calculator.schedule && !calculator.schedule.isInSchedule(dueDate)) {
    dueDate.add(calculator.schedule.whenNext(dueDate, calculator.timezone));
  }

  // Set calculated end time
  calculator.endDateTime = dueDate;
  calculator.calcScheduleDuration();
})();
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SohamJ 

what's your actual business requirement? share some screenshots.

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

View solution in original post

3 REPLIES 3

Mark Manders
Mega Patron

Why not just put the condition on the category? Your code will probably cause very weird behavior when it comes to pausing.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@SohamJ 

what's your actual business requirement? share some screenshots.

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

This is the solution? So you resolved it by getting the business requirements clear, do I understand that correctly?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark