function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sara DakinSara Dakin 

Permission to delete a record when status changes

If a user does not have permission to delete a record, but they need to be able to do so when the status changes (IE to closed), how do you give the user permission to delete that particular record?
AmrenderAmrender
Hi Sara

There are multiple ways to do it

1) You can use batch apex. The process will run behind the screen. Record will be deleted automatically once status changes to Closed.
2) Write an after update trigger on your object and delete the record using a DELETE dml operation.[this is also a backend process and your code should execute without sharing]
3) If you really want to give the delete premission to user [not recommened]
    a) give the delete premission on the profile
    b) create a new RecordType and PageLayout
    c) remove the delete button from one the layout and assign to the appropriate profiles

Please elaborate your question in more specific business requirement so that I could provide you a better solution.

Regards
Amrender
Krishna SambarajuKrishna Sambaraju
You can create a permission set to delete the object and assign it to user when the status changes through a trigger. 
PermissionSet ps = [select Id from PermissionSet where Name = '<permissionsetname>'];

PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = ps.Id, AssigneeId = UserInfo.getUserId());

insert psa;
and delete the PermissionSetAssignment in "after delete" trigger on the object (Assuming that the same user is going to delete the record).

PermissionSet ps = [select Id from PermissionSet where Name = '<permissionsetname>'];

PermissionSetAssignment psa = [select Id from PermissionSetAssignment where PermissionSetId = :ps.Id and AssigneeId = :UserInfo.getUserId()];

delete psa;
Hope this helps.