Background script vs scheduled job

ryadavalli
Tera Expert

Trying to run a query against v_cluster_transaction table in a scheduled job, doesnt seem to be working. Same script is working if run as background script:

var transactionCount = 0;
var gr = new GlideRecord("v_cluster_transaction");
gr.addQuery("age", ">=", gs.getDurationDate('0 0:0:1'));
gr.query();
while(gr.next())
{
transactionCount++;
}
if(transactionCount> 0)
{
gs.log(transactionCount + ' Transactions were cancelled');
}

1 ACCEPTED SOLUTION

Did you change the Variable name to something other than gr ?

As i suggested in my First Reply.

View solution in original post

9 REPLIES 9

sachin_namjoshi
Kilo Patron
Kilo Patron

do you see any error in progress worker of scheduled job after running this job?

How many records you get with your query?

 

Regards,

Sachin

ARG645
Tera Guru
Am gonna sound strange. But, change the varibale name gr to something else. It may work. Reason: i was told by servicenow to avoid using the varibale name gr for a Glide record Object in a scheduled Job, as it may overlap with some other global varibales. If you have to use gr, use it inside functions(that way it will be local)

Brent Sutton
Mega Sage

Hi ryadavalli,

If you are running this in a scheduled job you will need to use gs.info rather than gs.log. The script will fail if you use gs.log.

A better approach to return a count of transactions would be to use GlideAggregate. This is much more efficient to execute, especially on large data sets. I've created a code snippet below to demonstrate GlideAggregate.

//use GlideAggregate rather than iterating through results to get a count.
var ga = new GlideAggregate("v_cluster_transaction");
ga.addQuery("age", ">=", gs.getDurationDate('0 0:0:1'));
ga.addAggregate("COUNT");
ga.query();

var transactionCount = 0;

if (ga.next()) {
	transactionCount = ga.getAggregate("COUNT"); //return the aggregate count
	//log only if the transaction count is 0 or below
	if (transactionCount <= 0) {
		//scheduled jobs should use gs.info rather than gs.log
		gs.info(transactionCount + " Transactions were cancelled");
	}
}

Let me know if it works for you.

Brent

I disagree. gs.log statements can be used in scheduled Jobs. And they wont fail the scheduled Job execution. https://community.servicenow.com/community?id=community_question&sys_id=44cccfe5db9cdbc01dcaf3231f961905