187 views

Overview


You may have cases when you are trying to call business rule from schedule job to update some records but business rule is not updating correct records or not updating anything. This article will explain some ways to debug business rule in such cases.

Example


Lets take an example of 'Auto Cancel Change' scheduled job thats calling business rule, but somehow business rule is not getting the correct results or not updating the correct records. 

1. First you need to make sure business rule script is getting triggered, to test this put some debug statements in your business rule.

-- For example here is your script you have for 'Auto Cancel Change' business rule:  

function autoCancelChanges() {
 
gs.print("I am Here inside Business rule");  ---------> To make sure you are in business rule script
var ps = gs.getProperty('cv.change.autoclose.time');
var pn = parseInt(ps);
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-pn);
if (pn > 0) {
gs.print("I am Here inside First IF condition"); ---------------> To make sure you entered in first IF condition pn has some value
var gr = new GlideRecord('change_request');
gr.addQuery('state', '-5');
gr.addQuery('approval', 'not yet requested');
gr.addQuery('sys_created_on','>',gs.daysAgo(30));
gr.query();
while(gr.next()) {
gs.print("I am Here inside When condition"); ---------------> To make sure query got any record from the conditions we have 
gr.state = '4';
gr.comments = 'Change automatically cancelled after ' + pn + ' days in the New state.';
gr.active = false;
gr.update();
}
}
 
-- Once you  kick off the job, open System Log --> All (syslog table) to make sure which log statements is printing. This will tell you if business rule is triggered and where it failed.
 
2. Now lets say your business rule is failed in while loop, it never entered in while loop. That means the query conditions did not bring back any results from database and conditions need to be reviewed. To review conditions run print Glide record query results in System Definition --> Scripts - Background. Here is the GR query you can run:
 

var gr = new GlideRecord('change_request'); 
gr.addQuery('state', '-5'); 
gr.addQuery('approval', 'not yet requested'); 
var createddate = gr.addQuery('sys_created_on','>',gs.daysAgo(30)); 
gr.query();
var numrecord=gr.getRowCount();
gs.print("Number of Record Query is Getting :" + numrecord);

 
Running this script is getting 0 records, here is the output: 
*** Script: Number of Record Query is Getting :0
 
-- Now remove conditions one by one to make sure which condition has issues. In this case issue is with approval condition:
gr.addQuery('approval', 'not yet requested'); 

The value of approval 'not yet requested' is 'not requested' in choice list table. Make necessary change to query you are running and make sure you are getting some records now..

-- You can print more details to debug further details like 'gs.print(createddate);' to see if date you mentioned has the issues   

Additional Information


1. Here please read more about Scripts - background module:

https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptsBackground.html

Important Notes from above doc:

-- Administrators can use the Scripts - Background module to run arbitrary JavaScript code from the server.

-- Running free-form JavaScript can cause system disruption or data loss. ServiceNow does not recommend running free-from scripts from a production instance.

2. Here you can read more about Glide API's:

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_GS-minutesAgo_N

Article Information

Last Updated:2018-06-18 16:55:48
Published:2018-06-18