Business Rule not working as desired

MStritt
Tera Guru

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?

 

Business Rule_When to run.pngBusiness Rule_Advanced.png

25 REPLIES 25

The model.model_number should be changed to product.model_number then.

Were you able to get this working?

No. Haven't gotten it to work. I've updated the code by changing 2 locations where it says model.model_numer to product.model_number. When I create an entitlement using one of the Model Numbers in the BR, SM aaS does not get enabled (change 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('product.model_number','IN',model_numbers);
    gr.addQuery('active',true);
    gr.query();
    if (gr.next()) {
        if (model_numbers.includes(gr.product.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 {
            gr1.u_sm_aas = 'false';
        }
        gr1.setWorkflow(false);
        gr1.update(); // Update the record
    }
})(current,previous);

It looks like the ".includes" piece I was using doesn't work like normal javascript. I change this to use a for loop instead. Try this,

(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('product.model_number','IN',model_numbers);
    //gr.addQuery('active',true);
    gr.query();
    if (gr.next()) {
        for (var i = 0; i < model_numbers.length; i++) {
            if (gr.product.model_number == model_numbers[i]) {
                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 {
            gr1.u_sm_aas = 'false';
        }
        gr1.setWorkflow(false);
        gr1.update(); // Update the record
    }
})(current,previous);

Hi!

Unfortunately, still not working. When I create an Entitlement using a Model Number in the BR, the SM aaS is still not being enabled.

(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('product.model_number','IN',model_numbers);
    gr.addQuery('active',true);
    gr.query();
    if (gr.next()) {
        if (model_numbers.includes(gr.product.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 {
            gr1.u_sm_aas = 'false';
        }
        gr1.setWorkflow(false);
        gr1.update(); // Update the record
    }
})(current,previous);

 

The  code below that you initially sent did work when at least when I created one entitlement with one of the Model Numbers in the BR. It just didn't update the field (moving to false) when adding multiple entitlements and deactivating all of them. The SM aaS field didn't change to false (stayed true). This is before you added the logic for the model_number field (current).

(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)

 

Is the model number a reference or text field? If it is a reference field we will need to use the sys_id rather than the displayed value. I would check all of the field names and make sure the variables being passed to them are in the right format. 

I tested this script with different tables and fields, and it has worked as expected.