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

Hi Aman,

I think you're right about gs.log still working with scheduled script executions. I just retested my snippet and it worked with gs.log.

Most documentation suggests the use of gs.info or gs.error for logging statements in scheduled script executions. It's nice to know you can still use gs.log if you prefer it.

I've updated my snippet to reflect this:

//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) {
		//log the transaction count if nothing was cancelled
		gs.info(transactionCount + " Transactions were cancelled");
	}
}

I have a feeling it has to do with the table I am trying query, it is not giving the result while run as a scheduled job.

It is giving me the count as 0 when run as scheduled job vs  gave me count 2 when ran as a fix script ?

 

 

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

As i suggested in my First Reply.

Hi Ryadavalli,

I am getting consistent results on the incident table with GlideAggregate. I tested scheduled script execution, background scripts and fix scripts. All returned the same results.

I don't have much in the way of data in the "v_cluster_transaction" table in my developer instance so its a little hard to pinpoint the mixed results, based on script type, you are seeing.

Could it be down to something like the timing of when the various script types were executed? Seems very unusual to be getting mixed results running exactly the same script.

You could try wrapping the script in a function to make sure the variables are isolated but I'll be surprised if this makes a difference. Something like:

clusterTransactionCount();

function clusterTransactionCount() {
	//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) {
			//log the transaction count if nothing was cancelled
			gs.info(transactionCount + " Transactions were cancelled");
		}
		else {
			gs.info(transactionCount + " Transactions were counted");
		}
	}
}

Let me know if this helped.

 

I think it is working and has been working, I was missing on the active transactions.

Thanks,