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 are you saying business rule is not running as expected? If your code is running fine then whats the issue now?

It's a strange scenario, but if I set my business rule to run from the 

u_cmdb_ci_capability_provisioning table and do a dot walk condition so if 
current.u_recipient.u_ocir_shared_service_recipient CHANGES, nothing seems to get fired. But if I do it from the Department table so if current.u_ocir_shared_service_recipient CHANGES, the business rule runs, but I get the following logs:
matthew_hughes_0-1732031313355.png

As I said, I'm wanting to capture the current capability and provider of the record in the u_cmdb_ci_capability_provisioning table

Hi @matthew_hughes ,

 

If you are trying to update field value which is present in u_cmdb_ci_capability_provisioning table on change of u_ocir_shared_service_recipient which is present in Department table then

You have to write BR in Department table not.

 

In BR just log the value of provider and capability, seems from BR itself value is not going properly.

Hi @Runjay Patel 

I'm not sure what you mean by 
You have to write BR in Department table not.

 

Please can you explain?

In which table are you changing the value? Department right? then your BR should be written on Department table.