- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 09:27 AM
hi everyone,
I have a catolog item in which the duration time difference is calculated and populated when start and end date are picked up. I need a catalog client scripts, I have attached the screen to give you an idea what I need exactly.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 09:12 AM
Hi
The previous code did not work.
I have configured the code below, and it worked as I did want. Just keep in mind that use this code for both start date and end date on variable (not duration)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2024 12:04 AM
Hello,
You can try this script but consider refactoring it to match your field values.
var fromDate = g_form.getValue('start_date');
var toDate = g_form.getValue('end_date');
var fromDateObj = new Date(fromDate);
var toDateObj = new Date(toDate);
var timeDifference = toDateObj.getTime() - fromDateObj.getTime();
var daysDifference = timeDifference / (1000 * 3600 * 24);
var totalDays = Math.round(daysDifference);
g_form.setValue('duration', totalDays);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2024 12:43 AM
this should be a simple one. what did you start with and where are you stuck?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 11:17 AM
Hi Ankur,
I have typed this on catalog client scripts as @kkrushkov suggested me. However, it does not calculate and populate the time difference on the "duration" filed. (as it is in the picture I have attached)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 09:02 PM
Hello, @A Icen
You need to set a variable name here, such as the end date. Additionally, if you test it in the Service Portal, change the UI Type to All.