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

reshmapatil
Tera Guru

Hi,

 

It is not advised to use current.update within Business rule. It can lead to recursive business rules ( see Prevent recursive business rules )

If you need to update any field, you should use 'before' Business Rule. In case you have strong requirement to use 'ASYNC' BR, you may need to look for another approach/solution (e.g. call event from BR).

Or use Script include.

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**

in script include can i use current.update()?

Hi @Fabrizio Joaqui ,

 

No, we can't use 'current' in the script include.

We can pass the 'current' object from Business Rule in the script include it as a parameter.

 

Refer:

  1. https://www.servicenow.com/community/developer-forum/can-we-use-quot-current-quot-object-in-script-i...
  2. https://snprotips.com/blog/2019/12/18/can-script-includes-use-the-current-variable

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**