How to set start field on form to 3 business days from request creation date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-18-2024 10:15 AM
Suppose there is a catalog item webinar request , and on that form we have a field start date . I need to set this field to 3 business days from request creation date.Please help me with the script include and client script for above requirement
- Labels:
-
Architect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-18-2024 05:14 PM
Hello @Ankitha4
You can below sample code to calculate the business days
var startDate = new GlideDateTime(); //today's date
var days = 3; //adding 3 business days
var dur = new GlideDuration(60 * 60 * 8 * 1000 * days); //milli seconds
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //8-5 weekdays schedule sys_id
var end = schedule.add(startDate, dur); //adding the days
gs.info(end);
BTW, Did you tried anything?
Hope it helps
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-19-2024 01:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-21-2024 01:58 PM
@Ankitha4
You cannot directly write the above script in Client Side.
Perform GlideAjax call and do the above calcualtion in script include and return the value to the Client Script.
Let me know if you need more help
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā06-24-2024 01:36 AM
Hi @Ankitha4 here is what you can try directly in the client script. The script has been created as onload but feel free to change the type and field names based on your requirement
function onLoad() {
// Get the current date and time
var currentDate = new Date();
// Calculate 3 business days from the current date
var targetDate = addBusinessDays(currentDate, 3);
// Set the 'start_date' field (replace with the actual field name)
g_form.setValue('start_date', targetDate.toISOString().split('T')[0]);
}
// Function to add business days
function addBusinessDays(startDate, days) {
var count = 0;
var currentDate = new Date(startDate);
while (count < days) {
currentDate.setDate(currentDate.getDate() + 1);
// Check if it's a business day (Monday to Friday)
if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
count++;
}
}
return currentDate;
}
-Harneet Sital
Request you please mark my answer as helpful or correct based on the impact
Find all my ServiceNow articles here