The CreatorCon Call for Content is officially open! Get started here.

Updating a field using an After Business rule when previous value is the same

mohcine elbah
Tera Contributor

Hello,

While I was scripting in a business rule I came across this issue. I am testing through my personal instance, and I am sure there is no custom scripting causing the issue. 

Here is the situation : I have an After Business Rule which triggers when the state field in Incident table changes to Resolved (6), the script of the Business rule changes the state to Work in progress (2). 

The issue is : If the initial state is per example New (1) and then I change manually the state to Resolved (6) this triggers the Business Rule, and It changes the state to Work in Progress (2), until here all is working as expected. BUT, if the inital state is Work in Progress (2) and I change manually the state to Resolved (6) this triggers the Business rule, but it doesn't update the state's value to Work in progress (2) as expected, the state stays Resolved (6) (Even tough all the other fields are updated as programmed in the business rule)

 

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

	if(current.state == 6){
		current.state = 2;
		current.impact = 1;
		current.setWorkflow(false);
		current.update();
	}

})(current, previous);

Note : This issue can be fixed if I put a current.update() just before current.state = 2; and keeping the last current.update(), this is not a good solution.

Note 2 : I have tried changing the order of the business rule to 10000 and removing setWorkflow(false) and setAbortAction(true), all these didn't work.

 

Appreciate if someone has a better solution

Thank you 😀

14 REPLIES 14

in my testing, with that BR active, I don't see how an incident is ever resolved. You mention "conditions" but I don't see those described.

 

And the 'incident_state' and 'state' fields are not synchronized as is done OOB. You should be using a Before BR as was proposed by @Robert H 

 

Hello @mohcine elbah ,

 

Change the Business Rule type from After to Before, as shown in my screen shot. Change the Order to 100. Remove current.setWorkflow() and current.update().

Basically set everything up as shown in my example (which I have verified to work) but use a script for checking your advanced conditions.

 

Regards,

Robert

Hello @Robert H ,

Thank you for the additional help, I haven't clarified my need very well I think, both Business rules Before and Async are working well, our need is not to update the field (by any means necessary) but it's to do it well and as it suits our business needs, Before doesn't suit our need because I am doing a REST call in the Business rule and using Before doesn't give the best user experience. For my particular need, I used Async because it works for me as well, but I was wondering why the After business rule doesn't do the update as well. This is just to help other users and probably me if I have a case later where I should use ONLY after and no other business rule type. 

Appreciate your help 😀

jcmings
Mega Sage

Use the debugger -- Debug Business Rules (Detailed) -- to see if there is another conflicting BR firing. This will also show you where your current BR is failing.

SanjivMeher
Kilo Patron
Kilo Patron

Any reason you have used an after business rule. Setting the value after the value has been set doesnt makes sense.
You can just use onBefore business rule and remove setWorkflow(false) and current.update().

Keeping setWorkflow(false) will also not audit what happened really and can confuse the assignee, what is going on.


Please mark this response as correct or helpful if it assisted you with your question.