Delete CI Records using an Array

Richo1
Tera Expert

Hello,

 

I have a requirement to remove CI records using a fix script based on the object ID containing the Azure subscription ID i.e. 

 

/subscriptions/<sub_id>/resourceGroups/

 

I was thinking about using an array to store all of the subscription ids i.e.

 

var subID = ['sub_id'
,'sub_id'
,'sub_id']
 
and then using a query to search records with object id's containing the sub_id string. Can anyone guide me in the best way to do this?
 
Thanks in advance.
1 ACCEPTED SOLUTION

Hi, not quite sure I have understood correctly, did you mean the target string CONTAINS the sub_id?

var subID = ['sub_id','sub_id','sub_id'];

for(rec in subID) {
var ciCheck = new GlideRecord('cmdb_ci'); //or a specific CI table
ciCheck.addQuery('referencefield', 'CONTAINS', subID[rec]);
// add additional queries if required or use encoded query
ciCheck.query();

if(ciCheck.next() {
gs.info('Deleteing record ' + ciCheck.sys_id + ' name ' + ciCheck.name);
ciCheck.deleteRecord();
}
}

 

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, a few ways to do this, but try one of these (untested) should get you a result.

var subID = ['sub_id','sub_id','sub_id'];

for(rec in subID) {
var ciCheck = new GlideRecord('cmdb_ci'); //or a specific CI table

//2 query options
if(ciCheck.get('referencefield', subID[rec]) {
gs.info('Deleteing record ' + ciCheck.sys_id + ' name ' + ciCheck.name);
ciCheck.deleteRecord();
}
//or
ciCheck.addQuery('referencefield', subID[rec]);
// add additional queries if required or use encoded query
ciCheck.query();

if(ciCheck.next() {
gs.info('Deleteing record ' + ciCheck.sys_id + ' name ' + ciCheck.name);
ciCheck.deleteRecord();
}

//this could be quicker but less control/auditing and I would test carefully

var subID = ['sub_id','sub_id','sub_id'];
var myId's = subID.toString();
var myQuery = 'yourReferenceFieldIN' + myId's;

var ciCheck = new GlideRecord('cmdb_ci'); //or a specific CI table
ciCheck.addEncodedQuery(myQuery);
ciCheck.query();
ciCheck.deleteMultiple();

 

Richo1
Tera Expert

Hi Tony,

 

Thanks for this, I probably needed to be a bit more specific in that I need to look for a value within the reference field  string i.e. I need to find the <sub_id> portion using an array within the string "/subscriptions/<sub_id>/resourceGroups/"  this is the bit that I was struggling with.

 

Thanks.

Hi, not quite sure I have understood correctly, did you mean the target string CONTAINS the sub_id?

var subID = ['sub_id','sub_id','sub_id'];

for(rec in subID) {
var ciCheck = new GlideRecord('cmdb_ci'); //or a specific CI table
ciCheck.addQuery('referencefield', 'CONTAINS', subID[rec]);
// add additional queries if required or use encoded query
ciCheck.query();

if(ciCheck.next() {
gs.info('Deleteing record ' + ciCheck.sys_id + ' name ' + ciCheck.name);
ciCheck.deleteRecord();
}
}

 

Richo1
Tera Expert

Hi Tony,

 

This worked great thanks, I replaced the if statement with while, which game the results I was looking for.

 

while(ciCheck.next()) {
gs.log('Deleteing record ' + ciCheck.sys_id + ' name ' + ciCheck.name);
countRow++;
//ciCheck.deleteRecord();
    }