Regarding population of due date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
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 :
If my response helped mark as helpful and accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
small correction :
dayOfWeek != 6 && dayOfWeek != 7
This will correctly calculate according to your condition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2m ago
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);
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
