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

Make custom object field not editable for user.


      We have a requirement in our project where we have fields in our custom objects that we do not want the user to edit from the edit layout but the field should be editable by apex code . For example we have a status field which shows the status of the object and it should not be editable by the user but the code should be able to update this. This could probably be done using page layouts but it would be preferrable if we can do this any other way. I looked in the forums for any help on this but setting the page layouts or setting the permissions were suggested but is there any other way this could be done?Thanks in advance.




Apex Code can edit any field that is marked in the schema as editable (i.e. not a system read-only field, such as Id or LastModifiedDate). You can make the field read only using any means you would like to prohibit users from modifying the value, and it will not affect Apex Code. The most reliable means would to mark the field's field security as read-only (Setup > ... > "Object" > "Field" > Set Field Level Security). This will prohibit all but system administrators from editing the field (they always get to edit any field), even when using the Data Loader or other API integrations, yet still allow your custom code to perform whatever edits it needs to on the field.

Prafulla PatilPrafulla Patil

Not sure / Clear why page layouts will not serve your purpose here, still let me state a different approch here -


Create a new Record Type named 'Read Only' for the object whose layout will be read only for all the field and then based on your triggering event to make the object Read Only chage the Record Type. Thats it !!