How to stop recursive business rule

Fredo
Tera Contributor

Hi

 

I have an ask to create a parent Case as soon as a case has been created and make the relationship by updating parent filed.

 

For that I created the following BR

 

Async / Insert

 

var parentCase = new GliderRecord ('case');

parentCase.initialize();

var parentSysID = parentCase.insert();

 

current.parent = parentSysID;

current.update();

 

I have used all the below statements but the BR keeps on running and creating more records

 

current.setWorkflow(false)

parentCase.setWorkflow(false)

current.setAbortionAction(true);

parent.Case.setAbortionAction(true);

1 ACCEPTED SOLUTION

Assuming table "Task" has field "Created from":

1.png

then business rule 1:

- Table = sn_customerservice_case

- When = after

- Insert = true

- Filter Conditions = [ Created from ] [ is empty ]

- Script =

 

(function executeRule (current) {
	var parent = new GlideRecord('sn_customerservice_case');

	parent.newRecord();
	parent.u_created_from = current.getUniqueValue();
	// Maybe also copy other fields
	parent.insert();
})(current);

 

business rule 2:

- Table = sn_customerservice_case

- When = after

- Insert = true

- Filter Conditions = [ Created from ] [ is not empty ]

- Script =

 

(function executeRule (current) {
	var child = current.u_created_from.getRefRecord();

	if (child && child.isValidRecord() && isCase(child)) {
		child.parent = current.getUniqueValue();
		child.update();
	}

	function isCase (child) {
		return child.getRecordClassName() == current.getRecordClassName();
	}
})(current);

 

The result is:

2.png

 

Pictures of the BRs attached.

View solution in original post

9 REPLIES 9

Jace Benson
Mega Sage

I think based on what you wrote, the easiest way to would be to check if the case already has children before you insert another parent.

 

Something like might work

var isCurrentAParent = false;
var childCases = new GlideRecord('case');
childCases.setLimit(1);
childCases.addQuery('parent', current.getValue('sys_id');
childCases.query();
if(childCases.next()){
  isCurrentAParent = true;
}
if(isCurrentAParent == false){
  // nope not a parent, lets make a parent.
  var parentCase = new GliderRecord ('case');
  parentCase.initialize();
  var parentSysID = parentCase.insert();
  current.parent = parentSysID;
  current.update();
}

 

When you create a Case for the first time, it will always have parent empty.

So it will always be isCurrentAParent = false

 

Am I wrong?

Running this async runs it later.  But yea, any new case when created will run the rules and then when the async rule rules it will run this code later.  It might make more sense to wait.  

Personally, selfishly I'm curious what benefit there is of creating a parent case whenever a case is created.

-O-
Kilo Patron
Kilo Patron

No matter how you twist it, if you have to use current.update(); or .setWorkflow(), you are doing it wrong.

The are several options to do it better, but for sure you need at least two business rules: one that creates the parent and another that updates back the parent's unique value (sys_id) on the original Case.

E.g:

- establish a flag field (even create a new reference field on task*, like "Parent of" or "Created from" reference)

- 1st (after or async) business rule on insert creates a new cases if the flag field is empty

- 2nd (after of async) business rule on insert updates the record referenced in flag with its unique value.

 

Later edit:

*) on task, because such situation will for sure crop up later in case of other processes and you will be able to reuse the flag field for similar situation.