- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2023 02:12 AM
Hello,
I am currently using the following script (BR) to calculate the difference between the end date and the start date.
The field where I want to display this difference is in the Duration field on the change request form.
The backend name of the field where I'm calculating the difference is "calendar_duration," and its type is "Duration."
//Background script
var change = new GlideRecord('change_request');
change.addQuery('sys_id','c83c5e5347c12200e0ef563dbb9a7190');
change.query();
if(change.next())
{
var startdate = new GlideDateTime(change.start_date);
var enddate = new GlideDateTime(change.end_date);
var dur_seconds = gs.dateDiff(startdate, enddate, true);
gs.info(dur_seconds);
gs.info('Total difference in hrs ' + dur_seconds/3600);
}
//ANS of background script
*** Script: 91800
*** Script: Total difference in hrs 25.5
//Business rule
(function executeRule(current, previous /*null when async*/ ) {
var startdate = new GlideDateTime(current.start_date);
var enddate = new GlideDateTime(current.end_date);
var dur_seconds = gs.dateDiff(startdate, enddate, true);
var duration = dur_seconds / 3600;
//current.calendar_duration = duration;
//current.cab_recommendation = duration + ' hrs';
})(current, previous);
Please note that my script is functioning correctly; I have tested it in a background script. I just need assistance with populating the duration field in the correct format.
Any assistance in resolving this issue would be greatly appreciated.
Thanks and Regards
Kartik Magadum
Solved! Go to Solution.
- 1,142 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2023 02:34 AM - edited 09-08-2023 02:36 AM
Hello
I have arrived at a solution by using the following code:
var startDate = current.start_date.getGlideObject();
var endDate = current.end_date.getGlideObject();
current.calendar_duration = gs.dateDiff(startDate.getDisplayValueInternal(), endDate.getDisplayValueInternal(), false);
Thanks and Regards
Kartik Magadum
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2023 02:30 AM
Hello @Kartik Magadum ,
As you got the duration I guess in hrs , so now you just need to set it in proper format.
// Format "Days Hrs:MM:SS" e,g 0 23:00:00
var duration = dur_seconds / 3600;
//lets assume you got duration as 89
var cal_dur = "0"+ ' ' + duration + ":" +"00:00";
current.setValue('calendar_duration', cal_dur );
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2023 02:34 AM - edited 09-08-2023 02:36 AM
Hello
I have arrived at a solution by using the following code:
var startDate = current.start_date.getGlideObject();
var endDate = current.end_date.getGlideObject();
current.calendar_duration = gs.dateDiff(startDate.getDisplayValueInternal(), endDate.getDisplayValueInternal(), false);
Thanks and Regards
Kartik Magadum
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 07:24 AM
Hi Kartik,
The above code is not working for me because it is not throwing any error and not displaying any values in the duration field .May I know the table(task,change_request) which you have given for this business rule .
Please help me on this.
Regards,
Nandhini Muthu.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 07:58 AM
Please let me know did you make any changes in field type of duration .