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 

Apex trigger for the contact to be deleted only by owner

1
2
3
4
5
6
7
8
9
10
11
12
13
trigger Contactdel on Contact(before delete)
{
if(trigger.isbefore && trigger.isdelete)
{

for(Contact t: trigger.old)
{   

    if(UserInfo.getUserId() != t.ownerid){t.addError('Permission Denied, Please Contact the System Administrator');}}
    }
  

}

Have this above apex trigger so the users can delete the contacts that are only owned by them.
However an user is trying to Merge two contacts.
And he is getting an error message. "Permission Denied"
How do we adjust the code so this code bypass for certain users(System administrators).


Thank you!
AbhinavAbhinav (Salesforce Developers) 
Hi Advika,

How about adding conditon for Profile Name
 
trigger Contactdel on Contact(before delete)
{
if(trigger.isbefore && trigger.isdelete)
{
String profileName = [select Name from profile where id = :UserInfo.getProfileId()].Name;
for(Contact t: trigger.old)
{   
if(UserInfo.getUserId() != t.ownerid&& profileName != 'System Administrator')
{
t.addError('Permission Denied, Please Contact the System Administrator');
}}
    }
}
reference:
https://salesforce.stackexchange.com/questions/262393/add-check-of-user-profile-to-a-trigger

If it helps, Please mark it as best Answer.

Thanks!


 
Britto Fernandez 2Britto Fernandez 2
Suggest to add either
- a flag on user object and include it in condition
or
- add id's in static resource and custom object and include it in condition