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
Akash Choudhary 17Akash Choudhary 17 

Apex Class and Trigger for Opportunity Record Lock

Hello Everyone,

I have a requirement wherein I need to make a Opportunity record read only when the opportunity stage reaches to Closed won or either Closed Lost.
Any help is much appreciated.

Thanks
rajavardhan shontireddyrajavardhan shontireddy
@Akash Choudary,
Use a record type(Read only )  assign a page layout with all fields set to read only .

  
Akash Choudhary 17Akash Choudhary 17
Hi Rajavardhan,

Tha​nks for the reply but I mentioned it in the title that I want to achieve this requirement through Apex class and trigger.
GovindarajGovindaraj
Hi Akash,

Why not any OOB feature ?

Even you can go for validation rule, something like below,

AND(
   IsClosed = TRUE ,
   NOT($Profile.Id = "xxxxxxxxxxxx" ),
   NOT(RecordTypeId = "xxxxxxxxxxx")
   )

Thanks,
Govindaraj.S
Akash Choudhary 17Akash Choudhary 17
Hi Govindaraj, 

I want to achieve it via Apex class and trigger only as when you use validation rule the user have the error message once he saves it not before also in case of assigning a page layout to a record type then calling it through workflow also doesn't help because you still can change the required fields like opportunity name that's why I wanted to use Apex class and trigger for it . If you can help me achieve it via code that will be of great help 

Thanks for the reply 
Andrew GAndrew G
Hi Akash

With a standard trigger you will have no luck doing this.  If we think about the "events" for triggers, they are "insert" or "update" (and "delete").  Delete not quite relevant in this case.

For the trigger to fire, one of the events of insert or update will need to execute.  By their nature, this would mean that the record is in edit mode.

If you don't want to use recordtypes, then you are going to have to try doing something in Visualforce with a controller.

Regards
Andrew