+ Start a Discussion
rzsrzs 

Using trigger code is there way to make an opportunity editable only by admins

Hello,

 

Through my trigger code is there any way to lock an Opportunity so that only  users with system administrator profile can edit it and not others ?

 

Please help.

Thank You.

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

You can have some logic like this:

 

trigger lockOpportunity(before update, before delete){
Boolean notadmin =false;
User usr = [select Profile.Name from User where Id = :UserInfo.getUserID()];
if(usr.Profile.Name!='System Administrator'){
notAdmin=true;
}
for(Opportunity oppty:Trigger.new){
if(notAdmin){oppty.addError('Cannot modify.Record locked to System admins');}
}
}

You should also explore options to not have the Opportunity not editable by the non-admin users using sharing rules/apex managed sharing so that they can't even click on the "Edit"/"Delete" buttons.

 

 

All Answers

Anand@SAASAnand@SAAS

You can have some logic like this:

 

trigger lockOpportunity(before update, before delete){
Boolean notadmin =false;
User usr = [select Profile.Name from User where Id = :UserInfo.getUserID()];
if(usr.Profile.Name!='System Administrator'){
notAdmin=true;
}
for(Opportunity oppty:Trigger.new){
if(notAdmin){oppty.addError('Cannot modify.Record locked to System admins');}
}
}

You should also explore options to not have the Opportunity not editable by the non-admin users using sharing rules/apex managed sharing so that they can't even click on the "Edit"/"Delete" buttons.

 

 

This was selected as the best answer
rzsrzs

Thanks Anand ! :)