Calulate the Change Request Duration from adding the date and time.

User382122
Tera Contributor

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!!

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

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

View solution in original post

4 REPLIES 4

Anand Kumar P
Giga Patron
Giga Patron

Hi @User382122,

Its a business rule that run on u_change_management table and calculate start date and end date.

 

Thanks,

Anand

Danish Bhairag2
Tera Sage
Tera Sage

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

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

Anubhav24
Mega Sage
Mega Sage

Hi @User382122 

To calculate duration between two dates  I recently used below script , you can reuse this:

var date1 = new GlideDateTime(<dat1>);
        var nDate1 = date1.getNumericValue();
       
        var date2 = new GlideDateTime(<dat2>);
        var nDate2 = date2.getNumericValue();
var ndiff = nDate2-nDate1;
var gdt = new GlideDateTime();
            gdt.setNumericValue(ndiff);