Parameters within Script include not generating any results

matthew_hughes
Kilo Sage

I've got a script include with the following function:

 
currentData: function(spcRecord, currentCapability, currentProvider, currentRecipient, currentStatus) {
            gs.log('MH The current UBS record is ' + spcRecord);
            gs.log('MH The current Capability is ' + currentCapability);
            gs.log('MH The current Provider is ' + currentProvider);
            gs.log('MH The current Recipient is ' + currentRecipient);
            gs.log('MH The current Status is ' + currentStatus);

            var retired = 7;

            var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecords.addQuery('u_provider', currentProvider);
            spcRecords.addQuery('u_capability', currentCapability);
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('sys_id', '!=', spcRecord);
            spcRecords.setLimit(10);
            spcRecords.query();
            gs.log('MH Number of records found: ' + spcRecords.getRowCount());

            while(spcRecords.next()) {
                gs.log('The Sys ID of this record is ' + spcRecords.sys_id);
            }
        },

        previousData: function(spcRecord, previousCapability, previousProvider, previousRecipient) {
            gs.log('MH The current UBS record is ' + spcRecord);
            gs.log('MH The previous Capability is ' + previousCapability);
            gs.log('MH The previous Provider is ' + previousProvider);
            gs.log('MH The previous Recipient is ' + previousRecipient);
        },
 
The script for my business rule is:
var currentSPCDetails = new LBGSPCSharedServicefield().currentData(current.sys_id, current.u_capability, current.u_provider, current.u_recipient, current.install_status);
 
The business rule triggers if either the recipient, capability or provider fields change. What I've found is that if I change either of the fields, the following logs appear:
matthew_hughes_0-1731585789580.png

 

However, I've also tried copying the function into a background script:

var retired = 7;
 
var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecords.addQuery('u_provider', '7e4692b81bae9550d1c4dbd6b04bcbef');
            spcRecords.addQuery('u_capability', 'd9155cc1dbb2b748d84b9a2adb9619c0');
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('sys_id', '!=', '4e08c3a11b0950103929971f2e4bcbe7');
spcRecords.setLimit(10);
            spcRecords.query();
gs.log('MH Number of records found: ' + spcRecords.getRowCount());
 
while(spcRecords.next()) {
gs.log('The Sys ID of this record is ' + spcRecords.sys_id);
}
 
When I run the background script, I get the required results:
matthew_hughes_1-1731586083317.png

 

 Can somebody explain where I'm going wrong because I want the parameters in my function to align correctly within my gliderecord 

 

 
12 REPLIES 12

First of all, in the Business Rule and Script Include you only need to include 'current' as a function parameter, then you will have access to every field on the record:

var currentSPCDetails = new LBGSPCSharedServicefield().currentData(current);
currentData: function(current) {
            gs.log('MH The current UBS record is ' + current.sys_id);
            gs.log('MH The current Capability is ' + current.u_capability);
            gs.log('MH The current Provider is ' + current.u_provider);
            gs.log('MH The current Recipient is ' + current.u_recipient);
            gs.log('MH The current Status is ' + current.install_status);
...

To get the same values as the ones you used in the background script, you need to change the recipient, capability, or provider on the same record.

 

Hi @Brad Bowman  , I've updated the Business Rule as suggested:

var currentSPCDetails = new LBGSPCSharedServicefield().currentData(current);
 
I've updated the Script Include as suggested:
LBGSPCSharedServicefield.prototype = {
        currentData: function(current) {
            gs.log('MH The current UBS record is ' + current.sys_id);
            gs.log('MH The current Capability is ' + current.u_capability);
            gs.log('MH The current Provider is ' + current.u_provider);
            gs.log('MH The current Recipient is ' + current.u_recipient);
            gs.log('MH The current Status is ' + current.install_status);

            var retired = 7;

            var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecords.addQuery('u_provider', current.u_provider);
            spcRecords.addQuery('u_capability', current.u_capability);
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('sys_id', '!=', current.sys_id);
            spcRecords.setLimit(10);
            spcRecords.query();
            gs.log('MH Number of records found: ' + spcRecords.getRowCount());

            while(spcRecords.next()) {
                gs.info('The Sys ID of this record is ' + spcRecords.getValue('sys_id'));
            }
        },
However though the log 'gs.log('MH Number of records found: ' + spcRecords.getRowCount());' is not showing on the logs. Do you know why that is?

In a list view or fix/background script, filter this table by the same u_provider, u_capability, excluding install_status 7, and the sys_id logged - making sure to use the same values as those logged.  When you do this are any records returned in the list view or logged in the background script? 

Hi @Brad Bowman  I've used the following background script with the same IDs for those fields:

var retired = 7;
 
var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecords.addQuery('u_provider', '7e4692b81bae9550d1c4dbd6b04bcbef');
            spcRecords.addQuery('u_capability', 'a9159cc1dbb2b748d84b9a2adb9619df');
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('sys_id', '!=', 'a60807a11b0950103929971f2e4bcb88');
spcRecords.setLimit(10);
            spcRecords.query();
gs.log('MH Number of records found: ' + spcRecords.getRowCount());
 
while(spcRecords.next()) {
gs.info('The Sys ID of this record is ' + spcRecords.getValue('sys_id'));
}
 
When I run it, I get the following:
*** Script: MH Number of records found: 5
*** Script: The Sys ID of this record is 2656dc23db5c60509941e18e0b9619dc
*** Script: The Sys ID of this record is 440803a11b0950103929971f2e4bcbb9
*** Script: The Sys ID of this record is 4e08c3a11b0950103929971f2e4bcbe7
*** Script: The Sys ID of this record is 5b0847a11b0950103929971f2e4bcbee
*** Script: The Sys ID of this record is 800803a11b0950103929971f2e4bcb8c

If these are the same 3 sys_ids that the logs show when running the Script Include, then it looks like the Script Include, or the user it is running as does not have access to your custom table, or does not have access to execute a GlideRecord on it.  Are the custom table, Business Rule, and Script Include all in the Global scope?  I don't know if this matters in this case, but your SI is missing the initialize function that is added by default to new scripts, so the beginning of the script should look like this:

 

var LBGSPCSharedServicefield = Class.create(); 
LBGSPCSharedServicefield.prototype = {
    initialize: function() {
    },

    currentData: function(current) {
...

 

It would be good to verify the user that the Script Include is executing as:

 

currentData: function(current) {
            gs.log('MH The script is running as ' + gs.getUserName());
...

 

add the same to the background script.  If both are executing as the same user, then there shouldn't be an ACL issue.  Does the user that the SI is executing as have the admin role, or the User role if one is specified in the Controls tab of the custom table definition?  If any of your components are not in the Global scope, is the custom table accessible from all application scopes in the Application Access tab of the table definition? 

After these changes, another thing you can try is to temporarily comment out the 4 addQuery lines so that you are running a GlideRecord on the entire table to confirm that works, then uncomment one line at a time and/or replace the script variables with the hard-coded sys_ids that you used in the background script until you find the one that is causing no records to be returned.