The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Business Rule Keep looping

ServNowDev
Tera Guru

im running a business rule to run after a value changes, but it keeps looping , i checking the values of two fields and send them in an event but this keeps looping which is causing me to have more than i need notifications.

 

var plannedHours = current.resource_plan.planned_hours;
var extention = current.resource_plan.extension_value;
var newTotalHours = plannedHours + extention;



gs.eventQueue('PlannedHoursOver10', current, plannedHours.toString(), newTotalHours.toString());
4 REPLIES 4

Harshal Aditya
Mega Sage
Mega Sage

Hi @ServNowDev ,

 

Hope you are doing well.

 

Could you please share the condition that you have in place for this BR

 

Regards,

Harshal

thanks for the reply here are my conditions 

 

Thomas42_0-1687841700432.png

 

Sagar Pagar
Tera Patron

Hi @ServNowDev,

 

As per the Scripts, there is no any loop/ recursion. make sure the correct condition is applied to business rule When to run sections.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Amit Gujarathi
Giga Sage
Giga Sage

HI @ServNowDev ,
I trust you are doing great.
Please find the updated code

(function executeRule(current, previous /*null when async*/) {
  // Check if the condition is met before proceeding
  if (current.resource_plan.planned_hours.changes() || current.resource_plan.extension_value.changes()) {
    // Update the fields without triggering the business rule again
    current.setWorkflow(false); // Disable workflow to prevent additional business rule execution
    current.resource_plan.new_total_hours = current.resource_plan.planned_hours + current.resource_plan.extension_value;
    current.update(); // Persist the changes to the record
    gs.eventQueue('PlannedHoursOver10', current, current.resource_plan.planned_hours.toString(), current.resource_plan.new_total_hours.toString());
  }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi