Compare one field value in a list with the field value from the next record in the list

Andrew Bettcher
Kilo Sage

Hi,

 

I have a list of records. I want to get a value from one field on the first record and see if it matches the value from the same field on the next record.

 

I can't work out how to get it to iterate to the next record. Using next() just goes through the list of records but it doesn't keep the original value. Here is my current script:

var grProtonDupe = new GlideRecord('u_cmdb_ci_proton_devices');
grProtonDupe.addEncodedQuery('u_device_type=Email^name=specific.email@address');
grProtonDupe.orderByDesc('serial_number');
grProtonDupe.orderByDesc('sys_created_on');
grProtonDupe.query();

//get the first serial number for comparison to the next one in the list

var currentSerial = grProtonDupe.serial_number; 
	var currentSysid = grProtonDupe.sys_id;

while (grProtonDupe.next()){

		var nextSerial = grProtonDupe.serial_number;
		var nextSysid = grProtonDupe.sys_id;
		
		gs.info(currentSysid + '    ' + nextSysid);
	}

I added sys_id because in my test data there are 3 records with the same serial number. My thinking is that I define my current sys_id and then move to the next() record so my two sys_ids should then be different but they aren't. 

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

You need to do the comparison inside the while loop, so here's one way to do that:

 

var prevSerial = ''; 
var prevSysid = '';
var grProtonDupe = new GlideRecord('u_cmdb_ci_proton_devices');
grProtonDupe.addEncodedQuery('u_device_type=Email^name=specific.email@address');
grProtonDupe.orderByDesc('serial_number');
grProtonDupe.orderByDesc('sys_created_on');
grProtonDupe.query();
while (grProtonDupe.next()){
    if (prevSerial == '') {
        prevSerial = grProtonDupe.serial_number; 
        prevSysid = grProtonDupe.sys_id.toString();
    } else if (prevSerial == grProtonDupe.serial_number) {
        gs.info('Serial # ' + prevSerial + ' ' + prevSysid + '  ' + grProtonDupe.sys_id.toString());
    }
}

 

 

View solution in original post

Sorry, I just realized I forgot the last 'else' to update the prev variables when the serial numbers don't match, so that it will detect the next set of dupes.

var prevSerial = ''; 
var prevSysid = '';
var grProtonDupe = new GlideRecord('u_cmdb_ci_proton_devices');
grProtonDupe.addEncodedQuery('u_device_type=Email^name=specific.email@address');
grProtonDupe.orderByDesc('serial_number');
grProtonDupe.orderByDesc('sys_created_on');
grProtonDupe.query();
while (grProtonDupe.next()){
    if (prevSerial == '') {
        prevSerial = grProtonDupe.serial_number; 
        prevSysid = grProtonDupe.sys_id.toString();
    } else if (prevSerial == grProtonDupe.serial_number) {
        gs.info('Serial # ' + prevSerial + ' ' + prevSysid + '  ' + grProtonDupe.sys_id.toString());
    } else {
        prevSerial = grProtonDupe.serial_number; 
        prevSysid = grProtonDupe.sys_id.toString();
    }
}

 

View solution in original post

8 REPLIES 8

You are welcome!

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew Bettcher 

to get the 1st one you need to use next()

var grProtonDupe = new GlideRecord('u_cmdb_ci_proton_devices');
grProtonDupe.addEncodedQuery('u_device_type=Email^name=specific.email@address');
grProtonDupe.orderByDesc('serial_number');
grProtonDupe.orderByDesc('sys_created_on');
grProtonDupe.query();

grProtonDupe.next();// this will actually take to 1st record
//get the first serial number for comparison to the next one in the list

var currentSerial = grProtonDupe.serial_number;
var currentSysid = grProtonDupe.sys_id;

while (grProtonDupe.next()){ // this will then iterate the 2nd onwards

var nextSerial = grProtonDupe.serial_number;
var nextSysid = grProtonDupe.sys_id;

gs.info(currentSysid + ' ' + nextSysid);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

BigZolo
Tera Expert

May I ask what's your goal with that?

@BigZolo Overall, I'm trying to de-dupe a huge number of CIs imported via discovery. This stage is about ensuring my overall script returns the same number of duplicates that I can via a grouped sort.

Your next question is why don't I use IRE? Well, the numbers are too high for me to reasonably be able to manage the de-dupe tasks that are generated. I'd be looking at well over 5000 tasks which would all need to be processed one by one. Also, the data is related to responses given in MRVS and non-MRVS variables which needs to be preserved. This cannot be achieved using the standard IRE process.

 

My plan is to break the data down via device type (about 15 different device types present) and process it through a series of fix scripts (or one script if I can).