Regarding population of due date

shreya_3009
ServiceNow Employee

Hello,
I have a requirement where when a catalog item is submitted it is stored as a request in a table.So we have a variable named due date where it stores the date when the request is created.But now i have a requirement that due date should be populated with created dated + 3 business days(excluding saturday, sunday).How can i do this, responses will be helpful

3 REPLIES 3

yashkamde
Tera Guru

Hello @shreya_3009 ,

 

This requirement can be done using BR
So create one before insert BR and write this code :

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var createdDate = new GlideDate();
    var dueDate = new GlideDate(createdDate);

    var daysToAdd = 3;
    var addedDays = 0;

    while (addedDays < daysToAdd) {
        dueDate.addDaysLocalTime(1);

        var dayOfWeek = parseInt(dueDate.getDayOfWeek());

        if (dayOfWeek != 0 && dayOfWeek != 6) {
            addedDays++;
        }
    }
    current.u_due_date_custom = dueDate;
	gs.log('DUE Date added - ' + dueDate);

})(current, previous);

 

Result :

Screenshot 2026-02-11 220427.png

 

If my response helped mark as helpful and accept the solution.

small correction :

dayOfWeek != 6 && dayOfWeek != 7


This will correctly calculate according to your condition.

Aditya08
Kilo Sage

Hello @shreya_3009 ,

 

im little bit confused here . you have to update field or variable ?

 

Im created one flow on x catalog item .Below custom action to update ritm record variable.No nned to create output in such custom action.

(function execute(inputs, outputs) {

var getDate = inputs.date;
var datee = new GlideDateTime(getDate);

var day = datee.getDayOfWeek(); 

if(day != 6 && day != 7) {

    if(day == 4) { // Thursday =>Monday
        datee.addDays(5);
    }
    else if(day == 5) { // Friday => Tuesday
        datee.addDays(6);
    }
    else {
        datee.addDays(3);
    }

}
else if(day == 6) { // Saturday =>Wednesday
    datee.addDays(5);
}
else if(day == 7) { // Sunday =>Wednesday
    datee.addDays(4);
}

outputs.due = datee; // set output AFTER calculation

var gr=new GlideRecord('sc_req_item')
if(gr.get(inputs.ritm))
{
    gr.variables.due_date=datee;
    gr.update();
}

})(inputs, outputs);

 

Below code is for updating field on record.IN this case we need output for sending code op for flow 

(function execute(inputs, outputs) {

var getDate = inputs.date;
var datee = new GlideDateTime(getDate);

var day = datee.getDayOfWeek(); 

if(day != 6 && day != 7) {

    if(day == 4) { // Thursday => Monday
        datee.addDays(5);
    }
    else if(day == 5) { // Friday =>Tuesday
        datee.addDays(6);
    }
    else {
        datee.addDays(3);
    }

}
else if(day == 6) { // Saturday =>Wednesday
    datee.addDays(5);
}
else if(day == 7) { // Sunday => Wednesday
    datee.addDays(4);
}

outputs.due = datee; // set output AFTER calculation



})(inputs, outputs);

Aditya08_0-1770832124173.png

 

Aditya08_1-1770832294682.pngAditya08_2-1770832505069.pngAditya08_3-1770832531558.png

 

Aditya08_4-1770832568154.png

 

 

Or if you want to apply this on every single request created in table so you can use this code in business rule :

 

 var getDate=current.sys_created_on;

var datee = new GlideDateTime(getDate);

var day = datee.getDayOfWeek();

if(day != 6 && day != 7) {

if(day == 4) { // Thursday => Monday
datee.addDays(5);
}
else if(day == 5) { // Friday =>Tuesday
datee.addDays(6);
}
else {
datee.addDays(3);
}

}
else if(day == 6) { // Saturday =>Wednesday
datee.addDays(5);
}
else if(day == 7) { // Sunday => Wednesday
datee.addDays(4);
}

current.due_date = datee; // set output AFTER calculation

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya