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
  • I would recommend using custom permissions for that, have you checked? Commented Aug 10, 2021 at 10:09

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.