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

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

 

Thank you for all these insights Carlos. I really appreciate it. I will try your recommendations and come back to this post soon with an update. Thanks again.

sunil maddheshi
Tera Guru

@AJ2025 
Try with below updated code:

var dateCr = new GlideDateTime('2025-02-14 07:00:00'); // Starting date
var days = 3; // Business days to add
var hours = days * 12; // Assuming 12-hour business days

// Fetch the schedule record properly
var schedRec = new GlideRecord('cmn_schedule');
schedRec.addQuery('name', "Test_Schedule"); 
schedRec.query();

if (schedRec.next()) { // Ensure the schedule record exists
    var sched = new GlideSchedule(schedRec.sys_id);
    var durToAdd = new GlideDuration(60 * 60 * 1000 * hours);
    
    var newDateTime = sched.add(dateCr, durToAdd);

    gs.info("New Business DateTime: " + newDateTime.getDisplayValue());

    // Example: Automatically update an RITM with the new date
    var ritm = new GlideRecord('sc_req_item'); // Replace with the actual RITM table if needed
    if (ritm.get('number', 'RITM12345')) {  // Replace 'RITM12345' with actual lookup logic
        ritm.work_end = newDateTime;
        ritm.state = 3; // Assuming '3' is Closed Complete
        ritm.update();
        gs.info("RITM " + ritm.number + " closed on " + newDateTime.getDisplayValue());
    }
} else {
    gs.error("Schedule 'Test_Schedule' not found!");
}

Please mark correct/helpful if this helps you!

Ankur Bawiskar
Tera Patron
Tera Patron

@AJ2025 

who is Configured as the Run as User in the scheduled job?

when script runs from scheduled job did you add logs?

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