Field value is not populating as expected in Case Table

tulasi8
Tera Contributor

Hi Community,

 

We have a requirement involving the Order table, where a field named “Total AMC Hours” stores the total number of AMC hours available for an order. We have introduced a new field on the Order record called “Remaining AMC Hours.”

Each Order is associated with an Asset, and each Asset can have multiple Cases linked to it. On the Case form, users log effort in the Time Worked related list.

The requirement is:

  • When a Time Worked entry is added on a Case,
  • And the Case state changes to Closed,
  • Then the Remaining AMC Hours on the associated Order should be updated as:

Remaining AMC = Total AMC Hours – Total Time Worked

We implemented a Business Rule on the Case table (After Insert/Update, condition: state changes to Closed).
Example:

  • Total AMC Hours on the Order = 20
  • Time Worked entered before closure = 5 hours
  • Remaining AMC Hours becomes 20 – 5 = 15

This works correctly on initial closure.   @Ankur Bawiskar 

Issue:
If a user revisits the Case after it is already closed and updates the Time Worked value (e.g., from 5 to 10 hours), the Remaining AMC Hours on the Order does not automatically recalculate to reflect the updated time (expected: 20 – 10 = 10).

We need guidance on how to modify the existing Business Rule script so that any update to Time Worked—even after the Case is closed—automatically recalculates and updates the Remaining AMC Hours accordingly.

Can someone please assist with the correct approach?



(function executeRule(current, previous /*null when async*/ ) {
 
    var asset = current.asset;
 
    if (!asset) return;
 
    var orderNumber = current.asset.u_order;
 
    var orderGR = new GlideRecord('csm_order');
    orderGR.addQuery('sys_id', orderNumber);
    orderGR.query();
    if (orderGR.next()) {
        var totalHours = parseInt(orderGR.u_amc_hr || "0");
 
        var csrHours = 0;
        var timeGR = new GlideRecord('task_time_worked');
        timeGR.addQuery('task.sys_id', current.sys_id);
        timeGR.query();
        while (timeGR.next()) {
            var duration = timeGR.time_worked.getGlideObject().getNumericValue();
 
            duration = duration / 1000;
            csrHours += duration / 3600;
 
        }
        var remHours = orderGR.u_remaining_no_of_amc_hours;
        if (remHours != '') {
            var remAmcHours = remHours - csrHours;
            var remainingAmc = parseInt(remAmcHours);
 
            orderGR.u_remaining_no_of_amc_hours = remainingAmc.toString();
            orderGR.update();
        } else {
            var remaining = totalHours - csrHours;
 
            var remaininghours = parseInt(remaining);
            //var hours = remaininghours.toString();
 
            orderGR.u_remaining_no_of_amc_hours = remaininghours.toString();
 
            orderGR.update();
 
 
        }
 
    }
 
})(current, previous);


Regards,
Tulasi.

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@tulasi8 

your current BR will run only once when State Changes To Closed

Update the BR condition as this state [IS] Closed

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar its already in state changes to closed, but missed that to update in the question in community

@Ankur Bawiskar its not working as expected for last requirement

@tulasi8 

which BR is not working?

1 BR is on Case and 1 is on other table? which other table

share that BR config screenshots

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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