- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 07:00 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 11:59 PM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 07:16 PM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 08:38 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 11:59 PM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 01:53 PM
Hi Tony,
This worked great thanks, I replaced the if statement with while, which game the results I was looking for.