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
Badal ShindeBadal Shinde 

Opportunity object having one Account field Scinario: if Account field /= empty and stageName = closed won then only sys admin edit the record and not the other useres

Best Answer chosen by Badal Shinde
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Badal,

Can you try the below trigger on Opportunity.
 
trigger OpportunityTriggerValid on Opportunity (before update) {
    Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
    For(Opportunity opp: Trigger.new){
        Opportunity oldoppy=Trigger.oldmap.get(opp.id);
       if(opp.AccountId==null && opp.StageName=='Closed Won' && opp.OwnerId!=oldoppy.ownerid && profileName!='System Administrator'  ) 
       {
           opp.adderror('cannot update closed won oppy');
       }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Karthik PadmakumarKarthik Padmakumar
Hi, 

You can set up a validation rules checking the conditions that you mentioned, which allows only the System Administrator to edit th record. 

Thanks,
KP
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Badal,

You mean if Account field is null and opportunity is closed won only system admina can edit it. If any other users tries it should throw error?

Can you clarify on it so can prepare the vallidation rule and share the same

Thansk,
 
Badal ShindeBadal Shinde
You mean if Account field is null and opportunity is closed won only system admina can edit it. If any other users tries it should throw error?
Yes, I know this happens in validation rule so I want to do this in trigger as well ?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Badal,

Can you try the below trigger on Opportunity.
 
trigger OpportunityTriggerValid on Opportunity (before update) {
    Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
    For(Opportunity opp: Trigger.new){
        Opportunity oldoppy=Trigger.oldmap.get(opp.id);
       if(opp.AccountId==null && opp.StageName=='Closed Won' && opp.OwnerId!=oldoppy.ownerid && profileName!='System Administrator'  ) 
       {
           opp.adderror('cannot update closed won oppy');
       }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer