Useful scheduling scripts

A business rule script specifies the actions that the business rule takes. Scripts commonly include predefined global variables to reference items in your system, such as the current record. Global variables are available to all business rules.

Caution: The customization described here was developed for use in specific instances, and is not supported by ServiceNow. This method is provided as-is and should be tested thoroughly before implementation. Post all questions and comments regarding this customization to our community forum.

Calculate duration given a schedule

Type: Before update/insert business rule

Table: Inicident

Description: A Business Duration calculates the Open to Close duration on an incident based on the particular Schedules. If there is no schedule specified, the script will simply use the first schedule returned by the query.

Script example:

The example below sets the resolved duration when the incident state moves to resolved.
if(current.incident_state==6){
  var dur = calcDurationSchedule(current.opened_at,current.sys_updated_on);
  current.u_resolved_duration= dur;
 
  function calcDurationSchedule(start, end){ 
  // Get the user   
  var usr =new GlideRecord('sys_user');
  usr.get(gs.getUserID());
  // Create schedule - pass in the sys_id of your standard work day schedule and pass in the users timezone
  var sched =new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae',usr.time_zone);
  // Get duration based on schedule/timezone 
  return(sched.duration(start.getGlideObject(), end.getGlideObject()));}

Check upcoming termination dates

Type: Scheduled script

Description: This script checks nightly for termination dates on contracts coming up in 90, 50, or 10 days (depending on the contract duration field).

Script example:

function contractNoticeDue(){
var gr =new GlideRecord("contract");
gr.addQuery("u_contract_status","Active");
gr.query();
while(gr.next()){
  if((gr.u_termination_date<= gs.daysAgo(-90))&&(gr.u_contract_duration=="Long")){
    gr.u_contract_status="In review";}
  elseif((gr.u_termination_date<= gs.daysAgo(-50))&&(gr.u_contract_duration=="Medium")){
    gr.u_contract_status="In review";}
  elseif((gr.u_termination_date<= gs.daysAgo(-10))&&(gr.u_contract_duration=="Short")){
    gr.u_contract_status="In review";}
  gr.update();}
 
}
Use scripts in business rules to accomplish common tasks such as:
  • Comparing two date fields.
  • Parsing XML payloads.
  • Aborting a database action in a business rule.
With scripts, you can also:
  • Specify the operation that triggers the business rule.
  • Use the scratchpad with display business rules to change form values just before a user loads the form.
  • Use the OR condition like you would in a condition builder.

You can also utilize the system's scripting functionality available for server-side scripts.

You can use options on the Business Rules form to build conditions, set field values, and display alert messages without needing to write a script.