How to duplicate Businee Elapsed Time to Custom Field?

Kazuma Akita
Tera Contributor

Hi community.

 

Does anyone have any insight on how to use a business rule to get the Business Elapsed Time value and store it in a custom field, Test Elapsed Time?

 

KazumaAkita_0-1718674846649.png

 

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @Kazuma Akita 

Business rule will run on Before or After you insert or update the Incident record. if you want to copy the business elapsed time in your custom field. then it will only update when u do some changes on the form.
When to Run: After - Insert/Update  both
Table : Incident

(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
    if (taskSlaGR.next()) {
        // Get the business duration (elapsed time) from the SLA in milliseconds
        var businessDurationMs = taskSlaGR.business_duration;

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

        // Store the exact duration in milliseconds in the custom duration field
        current.u_test_elapsed_time.setValue(businessDurationMs);
    } else {
        gs.log("No in-progress SLA found for task " + current.sys_id);
    }
	current.update();
})(current, previous);

try this code in Business rule. 

DeepakShaerma_0-1718685899641.png

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 

View solution in original post

5 REPLIES 5

Deepak Shaerma
Kilo Sage

Hi @Kazuma Akita 

Business rule will run on Before or After you insert or update the Incident record. if you want to copy the business elapsed time in your custom field. then it will only update when u do some changes on the form.
When to Run: After - Insert/Update  both
Table : Incident

(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
    if (taskSlaGR.next()) {
        // Get the business duration (elapsed time) from the SLA in milliseconds
        var businessDurationMs = taskSlaGR.business_duration;

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

        // Store the exact duration in milliseconds in the custom duration field
        current.u_test_elapsed_time.setValue(businessDurationMs);
    } else {
        gs.log("No in-progress SLA found for task " + current.sys_id);
    }
	current.update();
})(current, previous);

try this code in Business rule. 

DeepakShaerma_0-1718685899641.png

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 

Hi @Deepak Shaerma 
Thank you for your response.

I tried to implement it as you suggested, but the values were not displayed in the custom fields. Is there any reason for this?

KazumaAkita_0-1718691711755.pngKazumaAkita_1-1718691754201.png

 

@Kazuma Akita 
also make sure, your custom field type is also duration as same as SLA field.
Type ; Duration


check you correct backend name for the custom field and business elapsed field.
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 

@Deepak Shaerma 

I was able to achieve the requirement with the following script based on what received. Thank you very much!

(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);