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
AKenAKen 

Making a field "uneditable"

Hi All,

 

Is there a way to lock a field once a user populates it?  We have a date field that we use for forecasting, and once a rep commits a date to that field, we want to lock it from being "adjusted"...is that possible?

 

Thanks!

Alex

Yoel GluckYoel Gluck

There is no built in condition for providing a create perm but not update on a per field basis. The options you have are:

  1. Mark the date field FLS as read-only, remove the field from the edit page layout, and embed a VF page into that layout that uses a controller to enforce the write perm based on your needs.
  2. Use a trigger to make sure you can only change the date from NULL to another value, but if it is not NULL you can't change it.

Best,

Yoel Gluck

Product Security Team @ Salesforce.com

AKenAKen

Thanks Yoel.  As I am fairly new to being our salesforce.com admin, do you have a suggestion on how I could write that trigger, as option #1 is way beyond my experience?  Thanks again for the help!

Yoel GluckYoel Gluck

Here is an example trigger: (It will allow setting the date assuming the date was not set previously. In addition you could set the object to not allow a null value on the date field if you want the users to have to set the date when they create the record)

 

 

trigger myTriggerName on MyObjectName__c (before update) {
    // Iterate over the new objects before they are updated in the db.
    for (MyObjectName__c newObj : Trigger.new) {
        // get the corresponding current object and compare the current date value to the new date value
        MyObjectName__c oldObj = Trigger.oldMap.get(newObj.id);
        if (oldObj.MyDate__c != null && oldObj.MyDate__c != newObj.MyDate__c) {
            newObj.addError('Can\'t update the date field');
        }
    }
}

  

 

Best,

Yoel Gluck

Product Security Team @ Salesforce.com