Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Uncle Rob
Kilo Patron

This log statement only appears in your previousData function.
So I suspect even though you pasted the business rule code, its not THAT business rule that's running.
Something else is calling the previousData function instead of the currentData one

UncleRob_0-1731586851249.png

 

Hi @Uncle Rob 

I've just updated a record and the business rule has triggered. It seems that I was looking at the wrong area. It seems to now get the right results according to the logs:

 

However though, I need to know why it's not showing the results in my while loop:

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

If that loop isn't outputting anything then the likeliest cause is spcRecords.next() has not returned anything.  You could try putting this before the loop:
gs.log('Row count of spcRecords' + spcRecords.getRowCount();

I suspect that will be zero or undefined.  And if it is, now we have to figure out why its not finding anything.