Business Rule not working as desired
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 11:47 AM
We have a Business Rule that will mark a true/false box (SM aaS/u_sm_aas) on our Account form true, if the Account has an Active Entitlement with a certain Product Model Number (see attached screenshot/s). It is marking the box true when an Entitlement is created with one of the Product Model Numbers, but if it expires and the Entitlement changes to false, the SM aaS box remains true. I've tested this with just one Entitlement on the Account. So, it didn't need to check for other active/deactivated Entitlements with one of these Model Numbers.
So, the Business Rule needs to check all the Entitlements on the Account to see if any of the Part Numbers are listed. If it does, and any are Active, the SM aaS box should be true. If it checks to see if any Entitlements have any of those Model Numbers and all are not Active, then it needs to make the SM aaS box false. Can this be configured in the script? For example, whenever any Entitlement is added/inserted or updated, it checks for these conditions? Or, do you think this is better configured as a Scheduled Job/script? To run a few times per day? If script updates are required, can you provide sample code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 12:34 PM
Yes, Entitlement table (service_entitlement). When the Status (u_service_line_status) of the Entitlement is ACTIVE, the 'active' field on the Entitlement is true. When the Status of the Entitlement changes to EXPIRED, the 'active' field on the Entitlement is false. That's why we're using 'active' field on the Entitlement. With that said, will the code you provided still work? Or should I still remove the Active = true condition?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 12:37 PM - edited ā08-17-2023 12:43 PM
You will still need to remove the condition, otherwise the BR won't trigger when the record updates it's active state from TRUE to FALSE. Also, just for clarity, do you want the u_sm_aas field to update to true when ANY Product Model is found active, or when ALL of them are found active?
EDIT: Currently, the script I provided updates the u_sm_aas field to FALSE when ANY entitlement is found to be not active (ie. ALL entitlements must be active for the field to be set to true).
If you would like to modify it to include ANY entitlement being active, make the following changes
var allEntitlementsActive = true; -CHANGE TO-> var allEntitlementsActive = false;
gr.addQuery('active',false); -CHANGE TO-> gr.addQuery('active',true);
if (gr.next()) {
allEntitlementsActive = false; -CHANGE TO-> allEntitlementsActive = true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 12:43 PM
If the Account has any of the Product Model Numbers in the BR and are Active.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 12:46 PM
Try this, and let me know how it works,
(function executeRule(current, previous /*null when async*/) {
// Create Variables
var hasActiveEntitlements = false; // We will assume that all Entitlement are Active by default
var account = current.account; // Get the sys_id of the related account
// Query the entitlement table for any inactive record related to the account
var gr = new GlideRecord('service_entitlement');
gr.addQuery('account',account);
gr.addQuery('active',true);
gr.query();
if (gr.next()) {
hasActiveEntitlements = true;
}
var gr1 = new GlideRecord('customer_account');
gr1.get(account);
gr1.query();
if (gr1.next()) {
// If no results were found earlier, this will be true otherwise, it will be false.
if (hasActiveEntitlements) {
gr1.u_sm_aas = 'true';
} else {
gr.u_sm_aas = 'false';
}
gr1.setWorkflow(false);
gr1.update(); // Update the record
}
})(current,previous)
I don't have these tables/fields, so I can't test it thoroughly, however if you run into any issues, let me know!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 01:02 PM
Updated the code and removed the Active = True condition. When I create an Entitlement with one of the Model Numbers in the BR and Save, the SM aaS field changes to true. If I then change Active to false on the Entitlement, SM aaS stays at true, and doesn't change to false. I created 2 Entitlements with 2 different Model Numbers listed in the BR. When I change Active to false on both, SM aaS remains at true.