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:47 PM
Did I explain sufficiently? As long as one Entitlement with one of the Model Numbers in the BR is Active, then SM aaS is true. Account could have 8 Entitlements that match the Model Numbers in the BR, but only one is Active, then SM aaS is Active. If all of the 8 Entitlements are not Active, then SM aaS would be false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 12:49 PM
Yes. As long as one is Active, then SM aaS would be Active. All would have to be Inactive for SMaaS to be false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 01:02 PM
Final code would look something like this then, I forgot about the logic for the model_number field.
(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
var model_numbers = ['list','model','numbers','here']
// Query the entitlement table for any inactive record related to the account
var gr = new GlideRecord('service_entitlement');
gr.addQuery('account',account);
gr.addQuery('model.model_number','IN',model_numbers)
gr.addQuery('active',true);
gr.query();
if (gr.next()) {
if (model_numbers.includes(gr.model.model_number)) {
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)
After putting in the model numbers this should hopefully work like a charm, mark this as helpful/solution if it works!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 01:12 PM
Now when I create an Entitlement using one of the Model Numbers in the BR, the SM aaS field does not go to true.
(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
var model_numbers = ['list','model','numbers','here'];
// Query the entitlement table for any inactive record related to the account
var gr = new GlideRecord('service_entitlement');
gr.addQuery('account',account);
gr.addQuery('model.model_number','IN',model_numbers);
gr.addQuery('active',true);
gr.query();
if (gr.next()) {
if (model_numbers.includes(gr.model.model_number)) {
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-17-2023 01:22 PM - edited ā08-17-2023 01:23 PM
I had a typo on one of the lines, try fixing that and see if it works.
gr.u_sm_aas = 'false'; -CHANGE TO-> gr1.u_sm_aas = 'false';
I tested the logic with another table and the code is working for me.