Update field with a business rule Async

Fabrizio Joaqui
Mega Guru

Hi, I created a BR  Async that populates fields every time an HR Profile is created, but I read that using current.update() is not recommended, how can I populate these fields? because if I don't use the current.update() it doesn't populate them.

 

this is my code:

 

(function executeRule(current, previous /*null when async*/) {
var token;
var intUtils = new sn_hr_core.successFactorsUtils();
token = intUtils.getToken();
try{
	if(current.u_position_core_hr === ''){
		throw new Error('There is no position');
	}else{
	var r = new sn_ws.RESTMessageV2('success_factors_api', 'GETPOSITION');
    r.setRequestHeader("Authorization", "Bearer " + token);
    r.setStringParameter('value', 'code eq '+current.u_position_core_hr);
    var response = r.execute();
    var responseBody = JSON.parse(response.getBody());
    var httpStatus = response.getStatusCode();
	var arr = responseBody.d.results;
	//gs.info(JSON.stringify(responseBody));
	current.u_businessunit_posizione_core_hr = arr[0].businessUnit;
	current.u_department_posizione_core_hr = arr[0].department;
	current.u_division_posizione_core_hr = arr[0].division;
	current.u_jobcode_posizione_core_hr = arr[0].jobCode;
	current.u_location_posizione_core_hr = arr[0].location;
	current.u_paygrade_position_core_hr = arr[0].payGrade;
	current.u_customstring4_posizione_core_hr = arr[0].cust_ProfessionalPath;
		//current.update();
	}
}catch(e){
	var message = e.message;
	gs.info('Errore creates candidate: ' + message + 'User: '+current.number + ' ' + current.user.getDisplayValue());
		gs.eventQueue('sn_hr_core.error_get_position_hr_core',current,current.number,current.user.getDisplayValue());
}


})(current, previous);

 

 

6 REPLIES 6

SuhasPSalunkhe
Kilo Guru

1. You can use before BR if applicable for your use case

2. ELSE you can use script action and ij script action you can update the field which you want.

 

You can get script action info in servicenow docs.

Mike_R
Kilo Patron
Kilo Patron

I would suggest moving this to a flow instead.

 

See this article from SN

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0687840

 

Recommendations

While you can modify the current record using the normal current.setValue('field_name', 'value') method or via direct assignment using current.field_name = 'value';, and you can update the record using current.update(), this is strongly discouraged. It is not good practice to modify the current record outside of a before business rule.

If the architecture of your application calls for current.update() in a business rule, this indicates your application needs a redesign. It would point to a potential problem or incident waiting to happen, which could be avoided by only modifying the current record before it is written to the database.