- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎08-23-2022 12:32 AM
Hello Everyone,
I was trying to set dynamic [Due date] with [Date type] as relative on [Ask For Approval] action in flow designer based on some condition. I tried to search online on how can I achieve this requirement but had no luck. So, I started working on this and I was able to set dynamic [Due date] with [Date type] as relative on [Ask For Approval]. I am sharing my experience in this article so that it can be helpful to others in case if they have similar requirement.
To set the [Due date] dynamically with [Date type] as relative you will need to set the values for the key shown in below JSON object.
{"action":"cancel","date_type":"relative","date":"2022-08-22 23:23:26","duration":"1","duration_type":"minutes","schedule":"38fa64edc0a8016400f4a5724b0434b8","schedule_label":"24 x 7"}
You can use the script option available on [Due date] field to set it dynamically. In below exmaple I have created a simple flow for Standard Laptop catalog item and
Based on the below 2 check box we will set dynamic [Due date] with [Date type] as relative.
If [Adobe Acrobat] and [Adobe Photoshop] are checked then set the [Due date] as 3 minutes from current date and time
If anyone of [Adobe Acrobat] or [Adobe Photoshop] checkbox is checked then set the [Due date] as 2 minutes from current date and time
if none of these check box is checked then default the [Due date] as 1 minute from current date and time
Below is the script used to set the [Due date] dynamically
var dueDateJson = {};
dueDateJson["action"] = "cancel";
dueDateJson["date_type"] = "relative";
//you can set default date or can leave it blank;
dueDateJson["date"] = "";
// set default duration as 1 minute
dueDateJson["duration"] = "1";
dueDateJson["duration_type"] = "minutes";
dueDateJson["schedule"] = "38fa64edc0a8016400f4a5724b0434b8";
dueDateJson["schedule_label"] = "24 x 7";
var acrobatChecked = fd_data._1__get_catalog_variables.acrobat;
var photoshopChecked = fd_data._1__get_catalog_variables.photoshop;
var gdt = new GlideDateTime();
dueDateJson["date"] = gs.nowDateTime();
if (acrobatChecked && photoshopChecked) {
// set duration to 3 minutes
dueDateJson["duration"] = "3";
} else if (photoshopChecked || acrobatChecked) {
// set duration to 2 minutes
dueDateJson["duration"] = "2";
}
// return the string version of JSON Object
return JSON.stringify(dueDateJson);
TEST RESULTS:
1. If both [Adobe Acrobat] and [Adobe Photoshop] are checked then [Due date] is set as (i.e 3 minutes from created/current date)
Approval [state] will be changed to No Longer Required once the [Due date] is reached:
2. If anyone of [Adobe Acrobat] and [Adobe Photoshop] is checked then [Due date] for approval is set as (i.e 2 minutes from created/current date)
3. If none of these check box is checked then [Due date] is set as (i.e 1 minute from created/current date)
Note: Relative dates always treat days as 24 hours regardless of the days schedule you select. For example, if you create a due date that expires in 1 relative day, the due date will occur in 24 hours based on the schedule you select. For an 8-5 weekdays excluding holidays schedule, a 24-hour duration is the equivalent of 2 complete business days and 6 hours into the third business day. When working with schedules where the business day is less than 24 hours, consider using relative hours instead of days.
If you find this as helpful or helped you to learn something new, please do mark it helpful.
Thanks
- 4,255 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Very usefull, thanks.
How did you know to use JSON structure?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
THis should be included in Servicenow Documentation. Thank you very much
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for the detailed explainer! I've been tearing my hair out over this, as the Docs page says the Due Date field expects a Schedule DateTime, so I was going down the GlideScheduleDateTime rabbit hole!
The Docs page is absolutely rubbish for this particular Flow Action.