Unable to get previous value using BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 11:45 PM
Hello,
I am trying to perform some actions based on new value and previous value of BU code field present on user table.
I created after(update on some field value change) BR on user table and writing below code. However I am getting blank value in logs. Please suggest.
if (current.u_sf_business_unit_code.changes()) {
var prevBU = previous.u_sf_business_unit_code;
var newBU = current.u_sf_business_unit_code;
gs.log('previous BU', prevBU);
gs.log('newBU ', newBU);
try {
gs.log('BU changes ');
sn_fd.FlowAPI.getRunner().subflow('global.sml_internal_mover__business_unit').inBackground().withInputs(inputs).run();
} catch (ex) {
var message1 = ex.getMessage();
gs.error(message);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 02:19 AM
if (current.u_sf_business_unit_code.changes() || current.u_bu.changes()) {
var prevBU = previous.u_sf_business_unit_code;
var newBU = current.u_sf_business_unit_code;
gs.log('previous BU', prevBU);
gs.log('new BU', newBU);
try {
gs.log('BU changes');
sn_fd.FlowAPI.getRunner().subflow('global.sml_internal_mover__business_unit').inBackground().withInputs(inputs).run();
} catch (ex) {
var message1 = ex.getMessage();
gs.error(message);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 08:38 AM
The issue you're facing is that the previous object is not available in an after(update) business rule. The previous object is only available in after(insert) and after(delete) business rules.
To access the previous value of a field in an after(update) business rule, you need to use the gs.oldValue() function. For example:
if (current.u_sf_business_unit_code.changes()) { var prevBU = gs.oldValue('u_sf_business_unit_code'); var newBU = current.u_sf_business_unit_code; gs.log('previous BU', prevBU); gs.log('newBU ', newBU); try { gs.log('BU changes '); sn_fd.FlowAPI.getRunner().subflow('global.sml_internal_mover__business_unit').inBackground().withInputs(inputs).run(); } catch (ex) { var message1 = ex.getMessage(); gs.error(message); } }
Here's a breakdown of the code:
- current refers to the current record being updated.
- gs.oldValue('u_sf_business_unit_code') gets the previous value of the u_sf_business_unit_code field.
- current.u_sf_business_unit_code gets the new value of the u_sf_business_unit_code field.
- The rest of the code is the same as what you had before.
I hope this helps!
Do give a thumbsup if this works for you.
Thanks
Akshay Gupta