How to restrict user to delete records when related lists is not empty ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2023 12:13 AM - edited 04-11-2023 03:59 AM
Hi Everyone,
I have written delete ACL on cmn_location where class is not Workplace Location, but I need to add condition when Associated tables have any records then deletion rule should not be applicable.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2023 04:48 AM
If you want the delete button to be disabled, you need to put the condition in Delete UI Action button which checks for usage of location. You can have a script include which can check all these tables and return true or false. For example, you can use the following code for the script include and call it wherever you want like this,
new CustomLoationUtils().checkReference(PASS CURRENT LOCATIOn RECORD SYS ID HERE);
var CustomLoationUtils = Class.create();
CustomLoationUtils.prototype = {
initialize: function() {},
checkReference: function(location) {
//Array syntax ["Table Name", "Field Name]"
var tables = [
['cmn_location', 'parent'],
['sys_user', 'location'],
['cmdb_ci', 'location'],
['cmdb_ci_circuit', 'location']
];
tables.forEach(function(table) {
var gr = new GlideRecord(table[0]);
gr.addQuery(table[1], location);
gr.query();
if (gr.next()) {
return false; // Records found do not delete
}
});
return true; // Didn't found usage on any tables, you can delete
},
type: 'CustomLoationUtils'
};
Mark this answer Helpful 👍/ Accept solution ✔️if helped you resolve your issue.
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2023 05:37 AM - edited 04-07-2023 05:42 AM
I want to write acl for deleting records in cmn_location when user has test.role.admin, but when user want to try to delete location records if any linked associated tables are there, then delete button should be disabled. user can't delete those records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 03:55 AM
Hi @AnveshKumar M @Sandeep Rajput
written SI and calling in ACL still not working. could you provide any suggestions. Thanks
Script Include:
var FA_CustomLoationUtils = Class.create();
FA_CustomLoationUtils.prototype = {
initialize: function() {},
checkReference: function(location) {
//Array syntax ["Table Name", "Field Name]"
var tables = [
['cmn_location', 'name'],
['sys_user', 'location'],
['cmdb_ci', 'location'],
['incident', 'location'],
['sc_request', 'location'],
['pm_project', 'location'],
['cmdb_ci_computer', 'location'],
['cmdb_ci_printer', 'location'],
['cmdb_ci_ip_router', 'location'],
['cmdb_ci_ip_switch', 'location'],
['cmdb_ci_ups', 'location'],
['cmdb_ci_pdu', 'location'],
['u_network_subnet_location', 'u_location'],
['u_cmdb_ci_fa_circuit', 'u_location'],
['cmn_location', 'parent'],
['discovery_schedule', location]
];
tables.forEach(function(table) {
var gr = new GlideRecord(table[0]);
gr.addQuery(table[1], location);
gr.query();
if (gr.next()) {
gs.log('venu' + table[0] + '-- ' + table[1]);
return false; // Records found do not delete
}
});
return true; // Didn't found usage on any tables, you can delete
},
type: 'FA_CustomLoationUtils'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 04:12 AM
@venugopal s instead of passing 'current.sys_id' in quotes can you call the script include in your ACL script like the following.
answer=new FA_CustomLocationUtils().checkReference(current.sys_id);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 08:00 AM
@Sandeep Rajput ,
Its working, but it's need to delete location records when it don't have any associated records.
Thanks