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.

Handle Integer in GlideRecord

Kiran Patil3
Giga Expert

Hi,

I am trying to get the next number based on RowCount output. Here is the script

 

	var existingCI = new GlideAggregate('cmdb_ci');
	existingCI.addQuery('company','81fca4cbac1d55eb355b4b6db0e3c80f');
	existingCI.addAggregate('COUNT');
	existingCI.query();
	var records = 0;
	if (existingCI.next()) {
		records = existingCI.getAggregate('COUNT');
		gs.info(records);
if(records> 0) {records= records+1;} else {records= records+2;}
		gs.info(records);
}

Script output:

*** Script: 102
*** Script: 1021

The expected output is:

*** Script: 102
*** Script: 103
3 REPLIES 3

Muneeswar Kasar
ServiceNow Employee
ServiceNow Employee

Seems you are appending, rather you should convert to integer and sum the numbers.

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi, 

 

Use this script:

 

var existingCI = new GlideAggregate('cmdb_ci');
existingCI.addQuery('company','31bea3d53790200044e0bfc8bcbe5dec'); // This is my company sys_id. Replace your sysid
existingCI.addAggregate('COUNT');
existingCI.query();
var records = 0;
if (existingCI.next()) {
records = existingCI.getAggregate('COUNT');
gs.info(records);
if(records> 0) {
records= parseInt(records)+7;
} else {
records= parseInt(records)+2;
}
gs.info(records);
}

 

Thanks,
Ashutosh

Yogi3
Kilo Guru

Try below

 

var existingCI = new GlideAggregate('cmdb_ci');
existingCI.addQuery('company','81fca4cbac1d55eb355b4b6db0e3c80f');
existingCI.addAggregate('COUNT');
existingCI.query();
var records = 0;
if (existingCI.next()) {
records = existingCI.getAggregate('COUNT');
records =parseInt(records,10)
gs.print(records);
if(records> 0) {records= records+1;} else {records= records+2;}
gs.print(records);
}