0

I want to give delete access to some set of users in certain condition to some records. I don't want to do this using profiles/permission sets. I want to achieve this using apex. Could anyone please give me an idea to write a trigger?

2
  • You would need to create a field the user can check, and then they would "edit" the record to delete it. CRUD permissions are checked before a transaction runs, so there's no way the trigger can intercept a deletion that fails because of CRUD and record permissions. – sfdcfox Nov 8 '18 at 5:09
  • 2
    Please note that we are not a "code writing" service. You are expected to demonstrate some level of prior research and/or attempt at solving this problem before the question is considered on-topic. Please read about How to Ask, and then edit your question if you'd like us to help you further. – sfdcfox Nov 8 '18 at 5:11
2

Delete access to a record requires one of the these items:

  • Modify All
  • System admin
  • Owner of record
  • Higher in the role hierarchy than the current owner

Assuming that you don't want to use Modify All permissions for users and assuming the user is not a member of a role higher than the record's owner; you are left with ....

Platform Events

Once the user chooses the record to delete, publish a Platform Event using Process Builder, Visual Flow or Apex.

The platform event might look like this:

DeleteMe__e with custom field (type Text, length 18) Id_to_Delete__c

Define a consumer for the Platform Event (again, using Process Builder, Visual Flow, or Apex trigger). This consumer will execute as Automated process user (and such user has Modify All) and can delete the record in question identified by the Id in question.

3
  • You could still do this with a trigger, you just need a without sharing helper class. Platform events are not the only option. – sfdcfox Nov 8 '18 at 5:15
  • 1
    @sfdcfox - of course ....lately I've been seeing the world 100% through Platform Events -- the without sharing class should work just fine and I have used such to deal with Sites guest users not having Crud access to standard objects, for example. – cropredy Nov 8 '18 at 5:23
  • I must admit, I wish I had more use for PE, because they are so awesome, especially with the new lightning:empApi to basically get one-line access to them in Lightning components. – sfdcfox Nov 8 '18 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.