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
madhan bondalapatimadhan bondalapati 

i have a field on opportunity.if that is checked i should not edit the record

I have one feild on opportunity ,it is checked the record should not be edited.I
wrote trigger like this ,but it is showing some error.Could anyone help in this

trigger DisableEdit on Opportunity (before update)
{
   Set<Id> parentIds=new Set<Id>();
   for(Opportunity opp : Trigger.old)
   {
            parentIds.add(opp.Id);
   }   
    Map<Id, Opportunity> parentMap=new Map<Id, Opportunity>();
   parentMap.putAll([Select Id from Opportunity where Oppty_Status__c = 'Closed' and Id in :parentIds]);
     for(Opportunity opp : Trigger.old)
    {
            if (parentMap.get(opp.Id)!= null)
      {
               opp.addError('You don\'t have Previlege to Edit a Renewal after status is set to Closed');
               
      }

    }           
}

It is showing error message at the opp.adderror.When i checked the debug log it is not passing that line
Shikha AgashiShikha Agashi
Try following code:
 
trigger DisableEdit on Opportunity (before update)
{
   Set<Id> parentIds=new Set<Id>();
   for(Opportunity opp : Trigger.New)
   {
		If(opp.Oppty_Status__c=="Closed"){
                      parentIds.add(opp.Id);
		}	
   }   
    
	if(!parentIds.isEmpty()){
		for(Opportunity o: [Select Id from Opportunity where id in: parentIds]){
			 o.addError('You don\'t have Previlege to Edit a Renewal after status is set to Closed');
		}
	}	
}