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

Read only and accessing through apex

Hi All,

I have a basic question. If I mark the field "Read Only" when i am defining the field. Will my apex code be able to access it and set values? My requirement is the field should be updated only through apex code and not by any other means. Is just hiding the field from the page layout good enough?


If a field is read only for the profile that is executing apex code then it depends on the code.  If your code is marked 'with sharing' it will not be able to, but if its marked as 'without sharing' it will be able to.


It's usually not best practices to use 'without sharing', but if you have your class using 'with sharing' you can have an inner class or another class it references use 'without sharing'.


public class with sharing MyClass
  //profile available logic here

  public class without sharing InnerClass
    //code here that the user would not normally be able to execute