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
PrakkiPrakki 

How to restrict user to not to enter values or how to make field as readonly by using trigger

 

Hello,

 

Can anyone please share trigger code to restrict user to not enter values in the field as per condition.

 

 Its a bit urgent.

 

Prompt responses will be higly appreciated.

 

Thank you. 

 

sfdcfoxsfdcfox
trigger X on Y__c(before insert, before update) {
  for(Y__c record:Trigger.new) {
    if((Trigger.isInsert&&record.Z__c!=null)||(Trigger.isUpdate&&Trigger.oldMap.get(record.id).Z__c!=record.Z__c)) {
      record.Z__c.addError('Value is read-only');
    }
  }
}

You can do the same with a validation rule, but I presume you have a reason for asking this, so this is one possible solution. Your exact requirements may vary, but this is the basic form of such a trigger.

sushant sussushant sus
Hi, you can use this in trigger . you can check through profile whom you want to make it read only

please this code

in trigger
string name = [select name from profile where id = :userinfo.getProfileId()].name;

if(name == 'System Administrator)
//perform ur logic