Update field with a business rule Async
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 09:16 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 09:36 AM
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**
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2022 09:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 12:59 AM
in script include can i use current.update()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 01:04 AM - edited 12-15-2022 01:08 AM
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:
- https://www.servicenow.com/community/developer-forum/can-we-use-quot-current-quot-object-in-script-i...
- 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**