- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:21 AM
Hi, We have requirement where we need to delete Core Companies record where Vendor ID(custom field) is empty.
The only relationship we need to look for is in the Business Application(cmdb_ci_business_app) table. If the core company record is referenced in any of the 3 fields: manufacturer, vendor or publisher; the record can't be deleted.
Everything else has to be deleted. Please let us know how can this be achieved.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:26 AM
(function() {
var grCompany = new GlideRecord('core_company');
grCompany.addNullQuery('u_vendor_id'); // Assuming 'u_vendor_id' is the custom field for Vendor ID
grCompany.query();
while (grCompany.next()) {
var companySysId = grCompany.sys_id;
var grBusinessApp = new GlideRecord('cmdb_ci_business_app');
grBusinessApp.addQuery('manufacturer', companySysId)
.addOrCondition('vendor', companySysId)
.addOrCondition('publisher', companySysId);
grBusinessApp.query();
if (!grBusinessApp.hasNext()) {
gs.info('Deleting Core Company record: ' + grCompany.name);
grCompany.deleteRecord();
} else {
gs.info('Core Company record is referenced in cmdb_ci_business_app and cannot be deleted: ' + grCompany.name);
}
}
})();
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:34 AM
Hi,
You can try the below code
var grCompany = new GlideRecord("core_company");
grCompany.addNullQuery("u_vendor_id");
grCompany.query();
while(grCompany.next()){
var company = grCompany.sys_id
var grBussApp = new GlideRecord("cmdb_ci_business_app");
grBussApp.addCondition("manufacturer",company).addOrCondition("vendor",company).addOrCondition("publisher",company);
grBussApp.query();
if(grBussApp.hasNext()){
grCompany.deleteRecord();
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:26 AM
(function() {
var grCompany = new GlideRecord('core_company');
grCompany.addNullQuery('u_vendor_id'); // Assuming 'u_vendor_id' is the custom field for Vendor ID
grCompany.query();
while (grCompany.next()) {
var companySysId = grCompany.sys_id;
var grBusinessApp = new GlideRecord('cmdb_ci_business_app');
grBusinessApp.addQuery('manufacturer', companySysId)
.addOrCondition('vendor', companySysId)
.addOrCondition('publisher', companySysId);
grBusinessApp.query();
if (!grBusinessApp.hasNext()) {
gs.info('Deleting Core Company record: ' + grCompany.name);
grCompany.deleteRecord();
} else {
gs.info('Core Company record is referenced in cmdb_ci_business_app and cannot be deleted: ' + grCompany.name);
}
}
})();
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:34 AM
Hi,
You can try the below code
var grCompany = new GlideRecord("core_company");
grCompany.addNullQuery("u_vendor_id");
grCompany.query();
while(grCompany.next()){
var company = grCompany.sys_id
var grBussApp = new GlideRecord("cmdb_ci_business_app");
grBussApp.addCondition("manufacturer",company).addOrCondition("vendor",company).addOrCondition("publisher",company);
grBussApp.query();
if(grBussApp.hasNext()){
grCompany.deleteRecord();
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 08:15 AM
Hi @Mark Manders and @palanikumar
Thanks for your replies. Since both had similar scripts, I used one of them here to get row count. However, the rowcount output from this query is equal to the number of records from the first Nullquery. In such case I'm not able to figure out the exact number of records in question:
var grCompany = new GlideRecord("core_company");
grCompany.addNullQuery('u_vendor_id');
grCompany.query();
while(grCompany.next()){
var comp = grCompany.sys_id;
var grBussApp = new GlideRecord("cmdb_ci_business_app");
grBussApp.addCondition("manufacturer",comp).addOrCondition("vendor",comp).addOrCondition("company",comp).addOrCondition("u_hosting_provider",comp).addOrCondition("support_vendor",comp);
grBussApp.query();
if(grBussApp.hasNext()){
gs.print(grCompany.getRowCount());
// grCompany.deleteRecord();
}
}
Regards,
Prashant Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 10:28 PM - edited ‎05-28-2024 10:33 PM
Hi Prashant,
Realized an issue with if condition. Replace last three lines with the below code.
Note: If you want the count of Business applications where the company is referred then use grBussApp instead of grCompany. grCompany will always return same count as it holds the company record which is not changed inside the while loop
if(grBussApp.hasNext()){
gs.print(grBussApp.getRowCount());
// grCompany.deleteRecord();
}
Palani