Adding days to schedule

AJ2025
Tera Contributor

I have an existing schedule that excludes weekends and public holidays. I want to use this schedule and add 3 business days to starting date. It works in the background script but fails to execute as a scheduled script job. (I want to use the newDateTime to close complete a RITM.)

 

     var dateCr = new GlideDateTime('2025-02-14 07:00:00');
     var dateTd = new GlideDateTime();
     var days = 3;
     var hours = days * 12; 
 
    var schedRec = new GlideRecord('cmn_schedule');
    schedRec.get('name', "Test_Schedule");
     var sched = new GlideSchedule(schedRec.sys_id);
 
    durToAdd = new GlideDuration(60 * 60 * 1000 * hours);
 
    var newDateTime = sched.add(dateCr, durToAdd);
 
// newDateTime should display '2025-02-19 07:00:00' as 15th and 16th are weekend.
 
So I basically need to get it working as a scheduled job.
   
1 ACCEPTED SOLUTION

Carlos Petrucio
Mega Sage

Hi,

 

Your script looks fine for calculating business days using the GlideSchedule API. However, if it works in the Background Script but fails as a Scheduled Script Job, the issue might be due to:

 

  1. Execution Context: A scheduled job runs as a different user (typically system), so ensure that the cmn_schedule record is accessible.
  2. Delayed Execution: Scheduled jobs run asynchronously, so GlideDateTime and GlideSchedule operations might behave differently than in synchronous scripts.
  3. Scoped App Restrictions: If you're working in a scoped app, GlideSchedule may need specific permissions.

 

Try the following approach:

1. Ensure cmn_schedule is Retrieved Correctly

Make sure the schedule record exists by logging it:

 

 

var schedRec = new GlideRecord('cmn_schedule');
if (schedRec.get('name', "Test_Schedule")) {
    var sched = new GlideSchedule(schedRec.sys_id);
    gs.info("Schedule found: " + schedRec.sys_id);
} else {
    gs.error("Schedule not found");
    return;
}

 

 

 

2. Convert Script to Work in a Scheduled Job

Scheduled jobs run differently than Background Scripts. Explicitly set dateCr with the correct format and ensure logging for debugging.

 

 

var dateCr = new GlideDateTime('2025-02-14 07:00:00');
var days = 3;
var hours = days * 12; // Assuming 12-hour workdays

var schedRec = new GlideRecord('cmn_schedule');
if (schedRec.get('name', "Test_Schedule")) {
    var sched = new GlideSchedule(schedRec.sys_id);
    gs.info("Schedule found: " + schedRec.sys_id);
} else {
    gs.error("Schedule not found");
    return;
}

// Create duration
var durToAdd = new GlideDuration(60 * 60 * 1000 * hours);
gs.info("Duration to add: " + durToAdd.getDisplayValue());

// Calculate new date
var newDateTime = sched.add(dateCr, durToAdd);
gs.info("New Business DateTime: " + newDateTime.getDisplayValue());

// Example: Close Complete a RITM
var ritm = new GlideRecord('sc_req_item');
if (ritm.get('sys_id', 'YOUR_RITM_SYS_ID')) {
    ritm.state = 'closed_complete';
    ritm.work_end = newDateTime;  // Updating the work end time
    ritm.update();
    gs.info("RITM closed on: " + newDateTime.getDisplayValue());
} else {
    gs.error("RITM not found.");
}

 

 

 

Debugging Steps

  1. Run as Background Script: If it works in Background Scripts but fails in a Scheduled Job, check logs for missing permissions.
  2. Check System Logs (syslog table): Look for errors related to missing permissions (gs.error messages).
  3. Test Adding Time in Simple Format

 

var testSched = new GlideSchedule();
var newDateTime = testSched.add(dateCr, new GlideDuration('3 00:00:00'));
gs.info("New DateTime: " + newDateTime.getDisplayValue());

 

  • Run a Test Job Manually: Create a Scheduled Job, select "Run Once", and check execution logs.

If my answer helped you, please mark it as the correct answer so that other users can find it and encourage us to continue responding to the community actively!

 

Thanks!

- Carlos Petrucio

 

View solution in original post

6 REPLIES 6

Scheduled job is running as "System Administrator". 

Scheduled job is in "Global" scope.

GlideSchedule API is a scoped API. Can I access its methods from Global scoped scheduled job?

Thanks

AJ2025
Tera Contributor

Thank you Carlos, Sunil and Ankur. I will soon come back with some update and mark all appropriate responses as answers.