0

Can we use hierarchical custom setting to restrict a particular user from deleting object record. The profile has Delete permission on profile for that object. And it should not be changed as per the business. So is there a way to restrict the access for only one user?

1

1 Answer 1

1

Yes you can have a hierarchy custom setting created StopDelete Create a new field as : 'ObjectNames', have all objects for which you need to disable delete in a , comma separated value.

For eg: ARecord__c,Account and User as a lookup of any user.

Then in you trigger, from before delete context just add a check:

trigger ARecordTrigger on ARecord__c (before delete) {

    String currentUserId = UserInfo.getUserId();
    StopDelete__c stopDeleteTrigger = StopDelete__c.getValues(currentUserId);
    if (stopDeleteTrigger != null && String.isNotBlank(stopDeleteTrigger.ObjectNames__c) &&
        stopDeleteTrigger.ObjectNames__c.split(',').contains(ARecord__c.getSObjectType().getDescribe().name)) {
        List<ARecord__c> aRecords = (List<ARecord__c>) Trigger.new;
        for (ARecord__c aRecord : aRecords) {
            aRecord.addError('Cannot delete record');
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .