How to restrict user to delete records when related lists is not empty ?

venugopal s
Tera Contributor

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

venugopals_0-1680851522273.png

 

@Ankur Bawiskar 

12 REPLIES 12

@venugopal s ,

 

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'
};

 

AnveshKumarM_1-1680867980655.png

 

 

Mark this answer Helpful 👍/  Accept solution ✔️if helped you resolve your issue.

 

Thanks,

Anvesh

Thanks,
Anvesh

Hi @AnveshKumar M 

 

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.

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'
};

 

venugopals_0-1681210551234.png

 

@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);

@Sandeep Rajput ,
Its working, but it's need to delete  location records when it don't have any associated records. 
Thanks