Retrieve sys_id from array stocking unique Names

Nico12
Mega Sage

Hi,

We are trying to retrieve the unique values of a table and then stock their sys_id in an array.

I got the unique values of Software Names in an array but don't achieve to get their associate sys_id.

var arrCmdbSpkgID=[];
var arrCmdbSpkgName=[];
var uniqueSpkgName=[];
var uniqueSpkgID=[];
var uniqueArr = new ArrayUtil();

	var pkgLogiciel = new GlideRecord('cmdb_ci_spkg');
	pkgLogiciel.addEncodedQuery('install_statusNOT IN99,7');
	pkgLogiciel.query();
	while(pkgLogiciel.next())
	{
		arrCmdbSpkgID.push(pkgLogiciel.sys_id.toString());
		arrCmdbSpkgName.push(pkgLogiciel.getValue('name'));
	}

	uniqueSpkgName = uniqueArr.unique(arrCmdbSpkgName);

	for (var e = 0; e < arrCmdbSpkgName.length; e++) {
		if(uniqueSpkgName){ 
			uniqueSpkgID.push(arrCmdbSpkgID[e]);

		}

	}

Regards,

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Nico,

You could use your script down to the unique array without the ID push, then run another GlideRecord on that table with the addQuery line to see if the name is in that joined array, then push the sys_id of each returned record into your ID array, or simplify it with just one GR, doing the unique test with each result before pushing the ID to an array like this:

var arrCmdbSpkgID=[];
var spkgName = '';
var pkgLogiciel = new GlideRecord('cmdb_ci_spkg');
pkgLogiciel.addEncodedQuery('install_statusNOT IN99,7');
pkgLogiciel.orderBy('name');
pkgLogiciel.query();
while (pkgLogiciel.next()){
	if (pkgLogiciel.name != spkgName) {
		spkgName = pkgLogiciel.name;
		arrCmdbSpkgID.push(pkgLogiciel.sys_id.toString());
	}
}

View solution in original post

5 REPLIES 5

You are welcome!