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 

Only record owners and system admins can delete contact records

I'm using this code to So only system admins and Record owners can delete contacts. However it even prevents system admin from deleting.
System admin get the same error message. 
Any suggestios please.

trigger Contactdel on Contact(before delete)
{
if(trigger.isbefore && trigger.isdelete)
{
if(userinfo.getProfileID()!='00ej0000000yklW')

for(Contact t: trigger.old)
{   

    if(UserInfo.getUserId() != t.ownerid){t.addError('cannot delete record that own');}}
    }
  }

}

Thanks!
VinayVinay (Salesforce Developers) 
Hi Adhvika,

Try below code.
 
trigger Contactdel on Contact(before delete)
{
if(trigger.isbefore && trigger.isdelete)
{
    String pname = [Select Id,Name from Profile where Id=:Userinfo.getProfileId()].Name;
if(pname != 'System Administrator')
{ 
for(Contact t: trigger.old)
{   

    if(UserInfo.getUserId() != t.ownerid){t.addError('cannot delete record that own');}}
    }
  }

}

Do not hard code id's instead you can use names.

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar