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

Fredo
Tera Contributor

Giga

 

Thank you so much for your response

 

If you have time, could you write a bit of the code as an example? It will help me visualize it 

 

Many thanks in advance

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.

Fredo
Tera Contributor

That is a great job... thank you so much !!!

You're welcome 🙂

israelrr89
Tera Contributor

remove current.update(); 

 

quote from the developer portal: 

Do not use current.update() in a Business Rule script. The update() method triggers Business Rules to run on the same table for insert and update operations, potentially leading to a Business Rule calling itself over and over.

 

read more here:  https://developer.servicenow.com/dev.do#!/guides/utah/now-platform/tpb-guide/business_rules_technica...