+ Start a Discussion

How can i make a field readonly using trigger

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

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.

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.