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

Can we make it Field as Read only Through coding



Can we make it Field as Read only to particular profile Through coding?


If it is possible,let me know how can we do that.

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

Hi - You cannot do this through Apex. Can you share your use case why you would want to do this?


In case You have to do this - Only option could be metadata API..





If you want the field not to be changed by some profiles then you can use the Validation rules.

Check for the logged in user profile and IsChanged method for that field.


You can make a field read only through coding - you'd have triggers that fire when the record is created or updated and throw an error if the field in question is altered.  It wouldn't be good practice though, as you'd be replicating standard functionality (field level security) via apex code.


 I have a Scenario on Opprotunity object,



     Whenever a Stage field is "Closed won" Amount field should be readonly..

How can i acheive it..



Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

This can work for you - Create 2 record types, one for Closed One Opps and Other for Non Closed Ones and create corresponding page layouts. When Opportunity is marked as closed won, Set the Recordtype as "Closed One" and Closed Won Page layout will be assigned. On this layout have the amount field as read only. 


Let me know if this works or you need addtional details.



Rakesh Aggarwal


You can do this via a validation rule - check the stage and if its closed won, check if the amount has changed - if it has, reject the change.