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

Ankur Bawiskar
Tera Patron
Tera Patron

@Mohamed Elsayed

So it means that BR is not auto triggering

Until you manually save the form it means the resolved field is not getting populated whenever case is getting resolved

Did you check that?

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

Hi Ankur,

The resolved field gets populated, which auto-triggers the Business Rule (BR) and updates the field in the backend as expected but without saving it. To save the value in the field (Business Duration To Resolve), it requires a manual save. This doesn’t make sense to me since this is a before BR.

@Mohamed Elsayed 

Ideally the BR is before update so it should work

try this

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

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

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

Same issue, and no other business rules are clashing with this one. For me, It seems a timing issue where the saving happens before the field gets updated.