Business Rule Issue - Calculate Duration to Resolve

Mohamed Elsayed
Tera Expert

Hi everyone,

 

I’ve configured a business rule to calculate the duration between case creation and resolution. However, the “business duration to resolve” field remains empty after the case is resolved until I manually save the form. Does anyone have suggestions on how to fix this issue?

 

MohamedElsayed_0-1735565617313.png

 

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

    function calculateBusinessDuration() {
        var createTime = current.sys_created_on.getGlideObject();
        var resolveTime = current.resolved_at.getGlideObject();

        // Define the schedule
        var schedule = new GlideSchedule('1036f8e21b09c110779c6283b24bcb37');

        // Calculate the duration considering the business hours
        var duration = schedule.duration(createTime, resolveTime);

        // Convert the duration to a GlideDuration object
        var glideDuration = new GlideDuration(duration.getNumericValue());

        // Set the duration to the custom field
        current.u_business_duration_to_resolve.setValue(glideDuration.getValue());
    }
})(current, previous);

 Regards,

Mo

1 ACCEPTED SOLUTION

@Mohamed Elsayed 

can you try to use after update BR and then use current.update() just after setting the field value

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

@Mohamed Elsayed 

can you try to use after update BR and then use current.update() just after setting the field value

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

That works, thanks @Ankur Bawiskar 

@Mohamed Elsayed 

I hope there is no other BR which is stopping the above BR to set the value?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader