- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 06:33 AM
Here is a script that I am working with. However, it isnt working. I got this code from aco-worker and dont not undersand how I should execute this! Should I use a business rule or client script.
See code below:
(function calulationDuration (){
// ---Please configure the mandatory fields---
//the change ticket number----- i need for this to check for all ticket numbers
var CR= "CR0046103";
var table = 'u_change_management';
if (!(CR)){
gs.error('Scheduled Job: Calculate Duration: missing one or more mandatory values ');
returns;
}
var gr = global.MITHelpServerUtil.recordFor(TABLE, 'number', CR);
if(!gr){
gs.error(gs.getMessage('{0}: cannot find record "{1}"', ['Scheduled Job: Calculate Duration', CR]));
return;
//calculate the duration between the record's start and end dates:
var startDate = gr. start_date.getGlideObject();
var endDate = gr. end_date.getGlideObject();
var startDate = gs. dateDiff(startDate.getDisplayValueInternal(), endDate.getDisplayValueInternal(), false);
gs.warn(gs.getMessage('{0}: duration = "{1}" ', ['Scheduled Job: Calculate Duration', duration]));
}
})();
Thanks for all you help in advance!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 06:53 AM
Hi @User382122 ,
Where do you want to display that duration, like in some custom field or some message as an alert or info.
In which ever scenario it is, you can use the below code to achieve that. Below code will work only in Server Side so be vary of that. If there is a custom field which u want to update with remaining days left u can use this below code in a BR(after Insert/update). If you want to display as an alert or message you can use client script ,call Script include using Glide Ajax n display the output as Alert or Info message.
var gr = new GlideRecord('u_change_request');//please use proper backend name of table
gr.addQuery('number','CR0000001');//use proper number which is present in your instance
gr.query();
if(gr.next()){ // use if or while based upon the query u use in 2nd line. If its for 1 record use 'if', for many records use 'while'
var startTime = new GlideDateTime(gr.u_start_date);
var endTime = new GlideDateTime(gr.u_end_date);
var duration = GlideDateTime.subtract(endTime, startTime);
var days = duration.getDayPart();// this will give you the number of days from Start date to end date
}
Please mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 06:47 AM
Hi @User382122,
Its a business rule that run on u_change_management table and calculate start date and end date.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 06:53 AM
Hi @User382122 ,
Where do you want to display that duration, like in some custom field or some message as an alert or info.
In which ever scenario it is, you can use the below code to achieve that. Below code will work only in Server Side so be vary of that. If there is a custom field which u want to update with remaining days left u can use this below code in a BR(after Insert/update). If you want to display as an alert or message you can use client script ,call Script include using Glide Ajax n display the output as Alert or Info message.
var gr = new GlideRecord('u_change_request');//please use proper backend name of table
gr.addQuery('number','CR0000001');//use proper number which is present in your instance
gr.query();
if(gr.next()){ // use if or while based upon the query u use in 2nd line. If its for 1 record use 'if', for many records use 'while'
var startTime = new GlideDateTime(gr.u_start_date);
var endTime = new GlideDateTime(gr.u_end_date);
var duration = GlideDateTime.subtract(endTime, startTime);
var days = duration.getDayPart();// this will give you the number of days from Start date to end date
}
Please mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 09:31 AM
Hi Danish,
I want to calulate the planned start and planned end date to a custom field that I created called "Total duration"?
Thanks for the reply. I will try to test this out. I guess I am confused about how to transfer the days and time to that field
Alicia
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 11:04 AM - edited 10-05-2023 11:05 AM
Hi @User382122
To calculate duration between two dates I recently used below script , you can reuse this: