Total duration on Change Request

User382122
Tera Contributor

Hello all, 

 

I want to see about creating a field called "Total CR Duration".  With code that looks a little something like this: 

 

var srt = g_form.getValue(current.start_date); 

var end= g_form.getValue(current.end_date); 

 

if (start != "") & (end != ""){

 

// calulated the duration start + end to give the total total and have it calulate in the "Total CR Duration" field. 

}

 

New to developing. Please help! 

2 ACCEPTED SOLUTIONS

Anand Kumar P
Giga Patron
Giga Patron

Hi @User382122 ,

 

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    if (start && end) {
        var startDate = new GlideDate(start);
        var endDate = new GlideDate(end);
     var duration = new GlideDuration(startDate, endDate);
        var durationDays = duration.getDisplayValue();
        g_form.setValue('total_cr_duration', durationDays);
]

 

Thanks,

Anand

 

 

View solution in original post

Hi @User382122 ,

The script i have provide at client side if you want script in business rule use below script according to your requirement you can use before br.

    var start = current.getValue('start_date');
    var end = current.getValue('end_date');

    if (start && end) {
        var startDate = new GlideDate(start);
        var endDate = new GlideDate(end);
        var duration = new GlideDuration(startDate, endDate);
        var durationDays = duration.getDisplayValue();
        current.setValue('total_cr_duration', durationDays);
}

Kindly mark it as 'Helpful' and consider marking it as the proposed solution if it meets your requirements.
Thanks,

Anand

View solution in original post

5 REPLIES 5

Hi @User382122 ,

Try this below script i have tested in background script its working mark helpful and solution proposed if its works for you.

var start = '2023-09-01';/replace with current.start date variable
var end = '2023-09-15';/replace with current.end variable

if (start && end) {
    var startDate = new GlideDateTime(start);/
    var endDate = new GlideDateTime(end);

    var durationMs = endDate.getNumericValue() - startDate.getNumericValue();

    var durationDays = durationMs / (1000 * 60 * 60 * 24);

    gs.print(durationDays);
}

Thanks,
Anand