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

JenniferRah
Mega Sage

I'm not sure if "current" is a reserved keyword. Maybe try renaming it in your script include to something else? 

 

Or maybe you could edit your Business Rule to send 2 parameters like this:

 

var currentSPCDetails = new LBGSPCSharedServicefield().currentDepartment(current.u_provider, current.u_capability);

 

Hi @JenniferRah  In my business rule, I've done the following:

var currentSPCDetails = new LBGSPCSharedServicefield().currentDepartment(current.u_provider, current.u_capability);
 
In my script include, I've done the following:
 var test = new GlideRecord('u_cmdb_ci_capability_provisioning');
        //var provider = '41e2d84ddb32b748d84b9a2adb961941';
        //var capability = 'a9159cc1dbb2b748d84b9a2adb9619df';
        var retired = 7;
        test.addQuery('u_provider', provider.getValue('sys_id'));
        test.addQuery('u_capability', capability.getValue('sys_id'));
        test.addQuery('install_status', '!=', retired);
        //test.addQuery('sys_id', '29a7ac89db5d8414535098dadb96196b');
        test.setLimit(10);
        test.query();
        gs.log("MH The count is " + test.getRowCount());
 
However, when I make a change to the 'u_ocir_shared_service_recipient' the log states:
MH The count is 0
 
The department table does not have a field called capability or provider as they come from the u_cmdb_ci_capability_provisioning table

When you send the current.u_provider to the script include, it sends the sys_id, so you don't need to do provider.getValue('sys_id') in the addQuery statement. You just need to use provider and capability. You can do a gs.log statement above it to show what is being sent if you want to be sure you are getting the right info.

I just noticed you said this: 

 


The department table does not have a field called capability or provider as they come from the u_cmdb_ci_capability_provisioning table

If the current record that the business rule is running on doesn't have a capability or provider field, then none of this will work. You are trying to use that record to do your query, so I'm confused as to what you are trying to search on.