How to subtract days from a schedule

JPSS
Tera Contributor

In the RITM table we have a field called dur date

 

i want to subtact 3 working days from thet due date and get the new date. (Thenumber of days might varies)

 

Could any one help.. 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@JPSS 

this link has the solution

subtract time from schedule to update task due date 

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

Rajdeep Ganguly
Mega Guru


Sure, you can achieve this by using GlideDateTime API in ServiceNow. Here is a sample script to subtract 3 working days from a date in a RITM (Requested Item) record:

javascript
// Get the RITM record
var ritm = new GlideRecord('sc_req_item');
ritm.get('sys_id', 'your_sys_id_here'); // replace 'your_sys_id_here' with the actual sys_id

// Get the due date
var dueDate = new GlideDateTime(ritm.due_date);

// Subtract 3 working days
dueDate.addDays(-3);

// Update the due date in the RITM record
ritm.due_date = dueDate.getDisplayValue();
ritm.update();


Please note that this script does not take into account weekends or holidays. If you need to consider these as non-working days, you will need to use a script include or a business rule that checks for these conditions.

Here are the steps to implement this:

1. Navigate to System Definition > Script Includes.
2. Click New to create a new script include.
3. Name the script include and write the script to subtract working days from a date.
4. Save the script include.
5. Use the script include in your business rule or script to subtract working days from the due date.

Please replace 'your_sys_id_here' with the actual sys_id of the RITM record you want to update.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Casper6060
Mega Sage

I have developed a solution that i implemented in a custom flow designer action.

With this setup you can either add or subtract days from an initial date to get the next working date.
If you don't use a schedule it just uses normal calendar days.


But if you use a proper schedule with monday to friday where each day is a full available day and you have child schedules where things like vacations and so on are set as busy, then you can set the start date on a tuesday and subtract 2 days and it will say the next available work day is on the friday.

 

Basically the way it works is if we are adding dates to the initial date, increment the initial date and add 1 to the workingDays variable, and when we have a number in workingDays that is the same as the amount of days we are offsetting and the day is in the schedule, then we return the new date.

 

It works the same way for subtraction but it instead subtracts one day at a time.

 

A maxTries is set to 100 so we dont get an infinite loop in case of some sort of schedule setup error.

(function execute(inputs, outputs) {
    var schedule = new GlideSchedule(); // Initiate GlideSchedule object
    var oneDayInMilliseconds = 24 * 60 * 60 * 1000; // one day in milliseconds (this is used when subtracting via GlideDateTime)
 
    var scheduleLoaded = false; // Initiate schedule loaded check
    // If we have a valid schedule in our input
    if (inputs.scheduleId != "") {
        scheduleLoaded = schedule.load(inputs.scheduleId.getUniqueValue()); // load schedule via sys_id
        scheduleLoaded = true;
    }
    // If no schedule is loaded
    if (!scheduleLoaded) {
        gs.warn('No valid schedule provided. Using default schedule.');
        schedule.load('');
    }
   
    var offset = inputs.daysOffset;
    var currentDate = new GlideDateTime(inputs.initialDate); // Cast input date to a GlideDateTime object
    var direction = inputs.subtractValue == false ? 'up' : 'down'; // If subtract is false we will traverse up, otherwise we traverse down
   
    // Add the offset number of days to the initial date provided in the input
    // If we go up we just add the offset days, if we go down we multiply the offset days with the amount of milliseconds in a day
    if(!scheduleLoaded){
        if (direction === 'up') {
            currentDate.addDays(offset);
        } else {
            currentDate.subtract(oneDayInMilliseconds * offset);
        }
    }
 
    // If no schedule is loaded, just return the new date on the added / subtracted days
    if(!scheduleLoaded){
        var returnGdt = new GlideDateTime(currentDate);
        outputs.adjusteddate = returnGdt.getDate(); // 'YYYY-MM-DD'
    }
 
    var maxTries = 100; // Max number of tries to get a valid schedule date (to stop infinite loop if schedule is configure incorrectly)
    var validDateFound = false;
    // If a schedule is loaded we look for the next available date in that schedule
    if(scheduleLoaded){
        // Loop until we find a date within the schedule
        var workingDays = 0;
        for(i = 0; i < maxTries && validDateFound == false; i++) {
            var gdt = new GlideDateTime(currentDate);
            //gs.info('workingDays: ' + workingDays);
            //gs.info('Current date: ' + gdt.getDate().getByFormat('EEEE dd MMMMM'));
 
            if (schedule.isInSchedule(gdt)) {
                if(workingDays == offset){
                    validDateFound = true;
                    outputs.adjusteddate = gdt.getDate(); // 'YYYY-MM-DD'
                }
                workingDays++;
            }
            if(!validDateFound){
                // Shift by 1 day based on direction
                if (direction === 'up') {
                    currentDate.addDays(1);
                } else {
                    currentDate.subtract(oneDayInMilliseconds);
                }
            }
        }
    }
 
    // If no valid business day found and a schedule was provided
    if(validDateFound == false && scheduleLoaded){
        gs.error('No valid business day found within ' + maxTries + ' tries.');
        outputs.adjusteddate = null;
    }
})(inputs, outputs);