How to calculate duration type field?

Kazuma Akita
Tera Contributor

 The following script is a business rule that copies the values of Business Elapsed Time and Business Time Left into the custom fields Test Elapsed Time and Test Time Left.
 How should the script be modified if we want to put the value of Business Elapsed Time and Business Time Left multiplied by 3 into Test Elapsed Time and Test Time Left?

 

(function executeRule(current, previous /*null when async*/ ) {

    var taskSlaGR = new GlideRecord('task_sla');
    taskSlaGR.addQuery('task', current.sys_id);
    taskSlaGR.addQuery('stage', 'in_progress'); // Ensuring it’s the in-progress SLA
    taskSlaGR.query();

    // Check if an in-progress SLA is found
    while (taskSlaGR.next()) {
        // Get the business duration (elapsed time) from the SLA in milliseconds
        var busElapsedTime = taskSlaGR.business_duration;
        var busTimeLeft = taskSlaGR.business_time_left

        // Ensure busElapsedTime is not null or undefined
        if (!busElapsedTime && !busTimeLeft) {
            gs.log("No business duration or business time left found for task " + current.sys_id);
            return;
        }

        // Store the exact duration in milliseconds in the custom duration field
        taskSlaGR.u_test_elapsed_time.setValue(busElapsedTime);
        taskSlaGR.u_test_time_left.setValue(busTimeLeft);
        taskSlaGR.update();
    }

})(current, previous);

 

KazumaAkita_0-1718786062367.png

1 REPLY 1

Community Alums
Not applicable

Hi @Kazuma Akita ,

The issue in your script is the way you are handling the duration. The GlideDuration object needs to be converted to a numeric value (in milliseconds) before performing arithmetic operations, and then converted back to a GlideDuration object to store in duration type fields.

 

Please find the below updated script-

(function executeRule(current, previous /*null when async*/) {
    var taskSlaGR = new GlideRecord('task_sla');
    taskSlaGR.addQuery('task', current.sys_id);
    taskSlaGR.addQuery('stage', 'in_progress'); // Ensuring it’s the in-progress SLA
    taskSlaGR.query();

    while (taskSlaGR.next()) {
        // Get the business duration (elapsed time) and business time left from the SLA in milliseconds
        var busElapsedTime = taskSlaGR.business_duration.getNumericValue();
        var busTimeLeft = taskSlaGR.business_time_left.getNumericValue();

        // Ensure busElapsedTime and busTimeLeft are not null or undefined
        if (busElapsedTime == null || busTimeLeft == null) {
            gs.log("No business duration or business time left found for task " + current.sys_id);
            continue; // Continue to the next SLA record if values are not found
        }

        // Multiply the duration values by 3
        var testElapsedTime = busElapsedTime * 3;
        var testTimeLeft = busTimeLeft * 3;

        // Convert the multiplied values back to GlideDuration
        var testElapsedDuration = new GlideDuration(testElapsedTime);
        var testTimeLeftDuration = new GlideDuration(testTimeLeft);

        // Store the multiplied duration values in the custom duration fields
        taskSlaGR.u_test_elapsed_time.setValue(testElapsedDuration.getDisplayValue());
        taskSlaGR.u_test_time_left.setValue(testTimeLeftDuration.getDisplayValue());
        taskSlaGR.update();
    }
})(current, previous);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar