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
ganeshjujjuruganeshjujjuru 

How can i make a field readonly using trigger

Please any body help me how can i make a field readonly using triggers

bvramkumarbvramkumar
Suppose "readOnlyField__c" is the field you want make readonly, then, Write a before update trigger.

Check if the value of "readOnlyField__c" has changed like
if(trigger.new[0].readOnlyField__c != trigger.old[0]).readOnlyField__c)
{
trigger.new[0].addError('You can not change the value of readOnly__c');
}
Rahul SharmaRahul Sharma
Trigger, here could also help here.

But, I think you must keep triggers the last option(as they are difficult to maintain, compared to validation rules).
So, you can also achieve the same using some simple validation rules.
Vinit_KumarVinit_Kumar

Agreed with Rahul,

 

You should be preferring Validation Rules or Page Layouts to make a field Read only rather than using Triggers to do the same.