Transferring script from a background script to a script include and business rule

matthew_hughes
Kilo Sage

I'm trying the value of the Shared Service field of an SPC record based on the value of a field within a Department record. At the moment, I've got it working in a background script as below:

 

var test = new GlideRecord('u_cmdb_ci_capability_provisioning');
var provider = '1a1441c7c3fd1e909447fc37050131ae';
var capability = '42151005dbb2b748d84b9a2adb9619a4';
var retired = 7;
test.addQuery('u_provider', provider);
test.addQuery('u_capability', capability);
test.addQuery('install_status', '!=', retired);
test.query();
gs.log("The count is " + test.getRowCount());


if(test.getRowCount() > 1){
gs.log("The total number of records is " + test.getRowCount());
test.u_shared_service = true;
test.updateMultiple();
}

else if (test.getRowCount() == 1) {
while(test.next()){
gs.print('The value of the Shared Service Recipient field is ' + test.name);
if (test.u_recipient.u_ocir_shared_service_recipient == true) {
gs.log('This is true');
test.u_shared_service = true;
test.update();
}
else{
gs.log('This is false');
test.u_shared_service = false;
test.update();
}
}
}

 

However, what I'm wanting to do is transfer this functionality so that it works on a script include when it's triggered by a business rule. I've tried doing at as the following in my script include:

currentDepartment(current) {
        var test = new GlideRecord('u_cmdb_ci_capability_provisioning');
        //var provider = '41e2d84ddb32b748d84b9a2adb961941';
        //var capability = 'a9159cc1dbb2b748d84b9a2adb9619df';
        var retired = 7;
        test.addQuery('u_provider', current.u_provider);
        test.addQuery('u_capability', current.u_capability);
        test.addQuery('install_status', '!=', retired);
        test.query();
        gs.log("MH The count is " + test.getRowCount());


        if (test.getRowCount() > 1) {
            gs.log("The total number of records is " + test.getRowCount());
            test.u_shared_service = true;
            test.updateMultiple();
        } else if (test.getRowCount() == 1) {
            while (test.next()) {
                gs.print('The value of the Shared Service Recipient field is ' + test.name);
                if (test.u_recipient.u_ocir_shared_service_recipient == true) {
                    gs.log('This is true');
                    test.u_shared_service = true;
                    test.update();
                } else {
                    gs.log('This is false');
                    test.u_shared_service = false;
                    test.update();
                }
            }
        }
    },
 
The business rule to trigger this:
 
matthew_hughes_0-1732007704370.png

 

matthew_hughes_1-1732007733092.png

 

I'm wanting my my script include to refer to the current SPC records that are being updated by the affected department record. Does anyone know how I do this? The background script works because I've specified the Provider and Capability fields, but how do I do it in a script include where it refers to the current values?

18 REPLIES 18

So I've written it like this:

matthew_hughes_0-1732032408030.png

 

matthew_hughes_1-1732032430536.png

However, I need to know how do I get the value of Shared Service from the u_cmdb_ci_capability_provisioning table updated for the current record? At the moment, it only seems to work if I specify the actual sys ID of a u_cmdb_ci_capability_provisioning  record or the Capability and Provider set for a record.

I think I'm missing the overall vision. Here's what I *think* you're trying to do.

 

  1. When the "OCIR Shared Service Recipient" field changes on a Department record (cmn_department), you are running a business rule.
  2. That business rule runs a script include that is supposed to look up records on the "u_cmdb_ci_capability_provisioning" table that are not Retired and that have a Capability (u_capability) and Provider (u_provider) that match some variables on the Department record that was changed. 

Is that right? If not, then I think we need to start back at the beginning and either simplify the ask or explain exactly what piece of the code is failing. 

Hi @JenniferRah 

The Shared Service field should always be true if there are multiple records with the same Provider and Capability:

matthew_hughes_0-1732089476899.png

 

For the below example when there is only one record, the Shared Service field should be set to True if the u_ocir_shared_service_recipient field within the Recipient field is set to True. If the u_ocir_shared_service_recipient field is set to False, then the Shared Service field should be set to False.

 

matthew_hughes_1-1732089560996.png

 

 

So is the Recipient field a reference to the Department table? I'm trying to understand why the business rule is running on the Department table. It looks like you are running it whenever that u_ocir_shared_service_recipient field changes. 

 

So what is happening right now is that the u_ocir_shared_service_recipient field on the Department changes... Let's say it changes from false to true. 

 

So you want the business rule to run and pull any records on the "u_cmdb_ci_capability_provisioning" table with that same Department in the Recipient field??? Because that's not what the script is doing right now. Right now, you are trying to look up the records with a capability and provider, but you don't have that information on the Department table, so you can't do that lookup. 

 

That's where I think I'm confused.

 

From what you said above, I think your script should be doing this:

  • Look up records from u_cmdb_ci_capability_provisioning with the Recipient field the same as the current record that is passed to it.
  • If the current record's u_ocir_shared_service_recipient field is true, then set the Shared Service field to true in all those records found in the lookup above. 
  • If the current record's u_ocir_shared_service_recipient field is false, then for each record from the first lookup, do a second lookup based on that first record's capability and provider. If it finds more than one, set the Shared Services value to true. Otherwise set the Shared Services value to false. 

 

Does that seem right?