- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2022 06:22 AM
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;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2022 06:46 AM
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
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2022 06:33 AM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2022 06:46 AM
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
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2022 06:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 11:42 PM
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