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
Adhvika BalaAdhvika Bala 

Delete Records only based on ownership

I'm looking for options for this usecase.
"A user can delete a records can be deleted only if its owned by that user"
Any suggestions on how to do this.
Thank you!
ANUTEJANUTEJ (Salesforce Developers) 
Hi Advika,

I think you can have a trigger on that particular object and in case if the current userid is not the owner id you can add an error to the record.

you can check the below code for reference for example, if I am checking on task,

trigger tasktri on task(before delete)
{
if(trigger.isbefore && trigger.isdelete)
{
for(task t: trigger.new)
{if(UserInfo.getUserId() != t.ownerid){t.addError('cannot delete');}}
}
}

Let me know in case if there are any questions or errors.

Regards,
Anutej
Adhvika BalaAdhvika Bala
I added this Trigger to Contact.
But This shows the following error message even if i attempt to delete that Contact that i own.
"There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help."

And i see this message only after adding the trigger.
Please advise.

Thank you!
John KlokJohn Klok
Hello Adhvika,

If your profile is not system administrator. Please reach out your system administrator.
If your profile is system administrator .
1. Click on setup
2. and search from profile and click on profiles.
3. Naviagate to your profile - system administrator.
4. ( Iam assuming your update account record , if it is another object please search for your objects)

https://drive.google.com/file/d/1Lfc8HdJXzAUSwFk3vYRrYt5Zyy8ctfab/view

5. under standard object permission section . you need to have edit check box is enabled. if not click on edit button on page and update check box.

Kindly mark this as solved if the reply was helpful.

Thanks.