how to add 2 business days to a current date and time and place it in another field using schedule

Renu9
Tera Contributor

Hi All,

I am writing a script in Run script activity in workflow to add 2 business days to current date and time and place it in a field. But my script is not returning proper value. It is throwing an error

Could you please guide

 

var scTask = new GlideRecord("sc_task");
scTask.addEncodedQuery("state=2"); //if state is work in progress
scTask.query();
if (scTask.next()) {
   
var startDate= gs.nowDateTime();
var days = 2;
//assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days); 
var schedule = new GlideSchedule('f4db82011b36a010cb721f42b24bcb27'); //put your schedule sys_id here
 var end = schedule.add(startDate, dur);
scTask.field1=end;
    
}

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

hi @Renu

Try this:

var scTask = new GlideRecord("sc_task");
scTask.addQuery("request_item", current.sys_id); //added here
scTask.addEncodedQuery("state=2"); //if state is work in progress
scTask.query();
if (scTask.next()) {
var startDate= new GlideDateTime(); //changed here
var days = 2;
//assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days); 
var schedule = new GlideSchedule('f4db82011b36a010cb721f42b24bcb27'); //put your schedule sys_id here
 var end = schedule.add(startDate, dur);
scTask.field1=end;
scTask.update();  //added here
}

 

Thanks,

Murthy

Thanks,
Murthy

View solution in original post

5 REPLIES 5

Abhijit4
Mega Sage

Please try using GlideDateTime() instead of using gs.nowDateTime() as shown below,

var scTask = new GlideRecord("sc_task");
scTask.addEncodedQuery("state=2"); //if state is work in progress
scTask.query();
if (scTask.next()) {
   
var startDate= GlideDateTime();
var days = 2;
//assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days); 
var schedule = new GlideSchedule('f4db82011b36a010cb721f42b24bcb27'); //put your schedule sys_id here
 var end = schedule.add(startDate, dur);
scTask.field1=end;
 scTask.update();
}

Below article might help you to resolve your issue :

Add Business Days Using Schedules

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Murthy Ch
Giga Sage

hi @Renu

Try this:

var scTask = new GlideRecord("sc_task");
scTask.addQuery("request_item", current.sys_id); //added here
scTask.addEncodedQuery("state=2"); //if state is work in progress
scTask.query();
if (scTask.next()) {
var startDate= new GlideDateTime(); //changed here
var days = 2;
//assuming there are 8 business hours
days = days*8;
var dur = new GlideDuration(60 * 60 * 1000 * days); 
var schedule = new GlideSchedule('f4db82011b36a010cb721f42b24bcb27'); //put your schedule sys_id here
 var end = schedule.add(startDate, dur);
scTask.field1=end;
scTask.update();  //added here
}

 

Thanks,

Murthy

Thanks,
Murthy

Mahendra RC
Mega Sage

Hello,

Please check with below script:

var scTask = new GlideRecord("sc_task");
scTask.addEncodedQuery("state=2"); //if state is work in progress
scTask.query();
if (scTask.next()) {
var startDate = new GlideDateTime();
var days = 2;
var hours = 9; // put the hours like 9 hours means 1 days. If you want you can put 24 hours for 1 day
var duration = new GlideDuration(60 * 60 * 1000 * hours * days); 
var schedule = new GlideSchedule('your schedule sys_id'); //put your schedule sys_id here
var end = schedule.add(startDate, duration);
scTask.end_date.setDateNumericValue(new GlideDateTime(end).getNumericValue());
scTask.update();
}

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

Hello Renu,

Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

If you still need any further help or guidance on this then please update those on this question.

Thanks