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

I've now updated my business rule to be the following:

matthew_hughes_0-1732028359475.png

 

matthew_hughes_1-1732028387680.png

 

I've updated the parameters in my script include to be:

 

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

Hi @matthew_hughes ,

 

You are passing non-declare variable to addQuery which will not execute.

change from test.addQuery('install_status''!=', retired);

To test.addQuery('install_status''!=', 'retired');

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

Good catch, @Runjay Patel. I hadn't paid attention to the install_status statement. Since this is a custom table, I'm not sure of how it's set up, but the others in the CMDB are number fields, so you would need to use the corresponding number of the install_status in this addQuery statement, not the display name.

Hi @matthew_hughes and @JenniferRah ,

 

If you are using OOB status filed the use number 7. like below.

test.addQuery('install_status''!=', '7');

 

RunjayPatel_0-1732030387499.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel  I've already got variable declared:

Retired = 7

 

The code will run as expected if I specify the sys ID of a record from the 'u_cmdb_ci_capability_provisioning' table or the provider and capability from the ''u_cmdb_ci_capability_provisioning' table. I'm wanting the change to run if the 'OCIR Shared Service Recipient' field within the 'Recipient' field changes.