Script Help to exclude weekends

Jyoti Tripathi
Giga Guru

I have a custom True/False field, i wanted to make the field set as true when the Assignment Group for the case is empty for more than 1 business days..
Someone please help how to achieve this

13 REPLIES 13

Jyoti Tripathi
Giga Guru

@Ankur Bawiskar : please help

Sai Shravan
Mega Sage

Hi @Jyoti Tripathi ,

To automatically set a custom True/False field to "True" when the Assignment Group for a case is empty for more than one business day, you can use a Business Rule in ServiceNow. Here's an example

(function executeRule(current, previous,) {
  // Check if the Assignment Group is empty
  if (!current.assignment_group.nil()) {
    return;
  }

  // Check if the case has been empty for more than one business day
  var oneBusinessDay = 60 * 60 * 24; // Adjust if your business days have different durations
  var now = new GlideDateTime();
  var createdOn = new GlideDateTime(current.sys_created_on);
  var elapsedDays = GlideDateTime.subtract(now, createdOn).getNumericValue() / oneBusinessDay;

  if (elapsedDays >= 1) {
    // Set the custom field to "True"
    current.u_custom_field = true;
  }
})(current, previous);

 

When to Run : "Before" and "After"

Table : incident (if it is incident)

 

Regards,

Shravan

Please mark it as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan : The above code will not exclude weekends. Also Business Rule will work for this...how about a Schedule job?

Grzegorz Kaznoc
Tera Contributor

Firstly it is good to have this information how long your assignemnt group is empty. You can take it from audit table, but it is not efficient way to search this way, so you want to somehow mark this time to get it easly accesible. Separate column which is calculated from business rule may help. It would store time when group become unasigned or incident with empty group has been created. And once incident is assinged you can clear whis field by same business rule.

 

Once you have it you can schedule action to raise you flag for such incidents. You can in your previous business rule schedule event and write script action which will mark youd field true if in the meantime it was not assigned (your time field will tell you that). Alternatively you can write Scheduled Script Execution (sysauto_script) job to search incident table by your time field and set value of your flag with updateMultiple() method for your incident GlideRecord.

 

For excluding weekends you can use GlideSchedule API which can tell you about elapsed time based on some schedule (like excluding weekends)