How do I check if it is an update or insert in Business Rule Filter Conditions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 09:16 AM
I have a need to run the same Business Rule script for both record inserts and updates, but the conditions are different for inserts and updates. I know I could have two separate Business Rules, but then I'd have to duplicate the code or call a Script Include. I could also use Condition instead of Filter Conditions, but there are a bunch of things I need to check and it won't all fit in the Condition field. If there is a way to check the operation (like current.operation() == 'insert') in Filter Conditions, then I think that is the cleanest, most maintainable way to do what I need. Does anyone know how to do it?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2024 07:37 AM
Hi @R Caz
you can't directly use current.operation() within the Filter Conditions for a Business Rule. However, you can achieve your goal of running the same script for both record inserts and updates with different conditions by using a single Business Rule and incorporating logic to differentiate between inserts and updates.
In the Business Rule's script section, use current.operation() to determine whether the operation is an insert or an update and apply different logic accordingly.
(function executeRule(current, previous /*null when async*/) {
if (current.operation() == 'insert') {
// Logic for insert operations
if (/* your insert-specific conditions */) {
// Perform actions for insert
}
} else if (current.operation() == 'update') {
// Logic for update operations
if (/* your update-specific conditions */) {
// Perform actions for update
}
}
})(current, previous);
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution!✔️! If this helps you to understand.