The CreatorCon Call for Content is officially open! Get started here.

Incident Duration Calculation

Sneha Nair
Tera Contributor

Hello all,

 

I have a field "Impact duration" of type "date" and two other field "impact start" and "impact end" of type date/time. I want the impact duration to be calculated as impact start minus impact duration and i tried using Business rule and client script for the same and it is not working. Can someone suggest better solution.

 

2 REPLIES 2

Sid_Takali
Kilo Patron

Hi @Sneha Nair can you share what you have done for this? I believe you were trying "impact start" minus "impact end" if I'm not wrong?

I  have tried business rule , and client script , Impact End - Impact Start

Business rule : 

(function executeRule(current, previous /*, gdt, userId*/ ) {
    gs.addInfoMessage("Testing");
    var start = current.u_impact_start.getDisplayValue();
    var end = current.u_impact_end.getDisplayValue();
    gs.addInfoMessage("start" + start);
    gs.addInfoMessage("end" + end);

    if (start && end) {
        // Convert start and end times to JavaScript Date objects
        var startDate = new GlideDateTime(start);
        var endDate = new GlideDateTime(end);

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

        var dur = GlideDateTime.subtract(endDate, startDate); //the difference between gdt1 and gdt2
        gs.addInfoMessage(dur.getByFormat('hh:mm:ss'));
        var set = dur.getByFormat('hh:mm:ss');

        //  var duration = new GlideDuration();
        //duration.setNumericValue(durationMs);

        current.u_impact_duration = set;
        //dur.getDisplayValue().split(" ")[0];
    }
})(current, previous);
 
Client Script : (Onload)
function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

function calculateImpactDuration() {
    // Get the values of the "impact start" and "impact end" fields
    var start = g_form.getValue('u_impact_start'); // Replace 'u_impact_start' with the actual field name
    var end = g_form.getValue('u_impact_end'); // Replace 'u_impact_end' with the actual field name

    // Check if both "impact start" and "impact end" have values
    if (start && end) {
        // Use moment.js to calculate the difference in hours
        var startDate = moment(start, 'YYYY-MM-DD HH:mm:ss');
        var endDate = moment(end, 'YYYY-MM-DD HH:mm:ss');
        var durationInHours = endDate.diff(startDate, 'hours'true);

        // Update the "impact duration" field with the calculated value
        g_form.setValue('u_impact_duration', durationInHours.toFixed(2)); // Replace 'u_impact_duration' with the actual field name
    } else {
        // If either "impact start" or "impact end" is empty, clear the "impact duration" field
        g_form.setValue('u_impact_duration'''); // Replace 'u_impact_duration' with the actual field name
    }
}

// Call the function when the form loads
calculateImpactDuration();

// Add an onChange function to recalculate the "impact duration" whenever "impact start" or "impact end" changes
g_form.addOnChange('u_impact_start', calculateImpactDuration); // Replace 'u_impact_start' with the actual field name
g_form.addOnChange('u_impact_end', calculateImpactDuration); // Replace 'u_impact_end' with the actual field name