BR working only when current.update() is added

Swetha M
Mega Guru

HI,

 

I have quite a situation where, I have written an AFTER  INSERT BR and it worked fine and updated records in san diego.. But when the same BR did not update records in UTAH, I added the current.update() and it started updating records.

 

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

var usrarray = [];
var item = new GlideRecord('sc_req_item');
item.addQuery('request',current.sys_id);
item.addQuery('cat_item','57be3d29b7121010189e22b5de11a937');
item.query();
if(item.next()){
var almhw = new GlideRecord('alm_hardware');
almhw.addQuery('sys_id','IN',item.variables.assets.toString());
almhw.query();
while(almhw.next()){
usrarray.push(almhw.assigned_to.getDisplayValue());
}
current.short_description = "Hardware Asset Refresh Order for User(s) - " + usrarray;
}
})(current, previous);

 

 

Any specific reason?

5 REPLIES 5

BharathChintala
Mega Sage

@Swetha M  I hope you have written this as Before Business rule. 

 

Never write current.update() in any business rule.

 

try below for last lines

 

while(almhw.next()){
usrarray.push(almhw.assigned_to.getDisplayValue().toString());
}
current.short_description = "Hardware Asset Refresh Order for User(s) - " + usrarray.toString();
}

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Sayan Bardhan R
Tera Contributor

@Swetha M  It seems you have written the business rule on sc_request table. There are few considerations that you can make :- 

- For sc_request table, I would guess there is a workflow attached to it. In case there is, try changing the After Business Rule order to greater than 1000. 

- For more information on the execution order of scripts and engines, try checking this link out 

https://docs.servicenow.com/en-US/bundle/utah-application-development/page/script/general-scripting/...

- Try implementing an Async business rule (On Insert) on the sc_req_item table with the condition 

current.cat_item = 57be3d29b7121010189e22b5de11a937

Update the Request (sc_request) record Short description. 

For more information on the getRefRecord() function, check the below link 

https://servicenow.org.in/servicenow-tutotrial/getrefrecord-function-in-servicenow/

 

If this was helpful, please mark my answer as accepted solution, and give a thumb up.

Sayan Bardhan Roy

Kiran_45
Giga Guru

Hi @Swetha M ,

Although current.update() in BR is not a best practice you can use the same in the Async and after BR,

 

1) Use Async instead of after that gives instance more time.

or

2) Update the work flow or flow designer attached to it.

 

You got these two options too. 

Vishnu_X95
Tera Contributor

Hi Swetha,

 

Good day.

 

Using current.update() is not a best practise in Business rules. It can cause recursive issues.

I suggest you to create a Script Include and call the script Include from the Business rule.

Its very simple and follows the best practices.

 

 

new scriptIncludeName().myFunctionName();
//add this to the Business Rule. 

//add this to your script include
myFunctionName : function(){	
		gs.info('Test');	
	},

 

 

If this was helpful, please mark my answer as accepted solution, and give a thumb up.

 

Regards

Vishnu P