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
Mayank_JoshiMayank_Joshi 

Issue in Trigger : caused by: System.FinalException: SObject row does not allow errors

Hi , I have a below trigger (no compilation Error) .It gives  System.FinalException: SObject row does not allow errors .

 

trigger trigrnotes on Note (before update) {

if(trigger.IsBefore){

List <TriggerOnNotesObject__c> tt =[Select Id , Is_Closed__c from TriggerOnNotesObject__c where Is_Closed__c=True ] ;
List<Note> Ntes = [Select Id , ParentId from Note where ParentId IN : tt ] ;

for (TriggerOnNotesObject__c CstObj : tt ){
if(CstObj .Is_Closed__c) {

for(Note n :Ntes )
CstObj.addError('test');

}
}

}
}

 

Alternatively , i have used (it is working fine but not according to my requirement , atleast it is showing error message )

 

for (TriggerOnNotesObject__c CstObj : tt ){
if(CstObj .Is_Closed__c) {

for(Note n :Trigger.new )

n.addError('test');

 

 

My requirement is to restrict users from editing Notes for SOject whose field Is_closed is True (checked) .Please Guide me . 

 

Thanks .

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I think the issue here is that you are trying to attach an error to a record that is not part of the trigger, which doesn't really make sense.  To return an error to the user you'll need to attach an error to the note that they are trying to update.

 

Off the top of my head, I think you need something closer to:

 

trigger trigrnotes on Note (before update) 
{
   Set<Id> parentIds=new Set<Id>();
   for (Note nt : trigger.new)
   {
      parentIds.add(nt.ParentId);
   }
  
   // create a map of all parent objects that are closed, keyed by id
   Map<Id, TriggerOnNotesObject__c> parentMap=new Map<Id, TriggerOnNotesObject__c>();
   parentMap.putAll([Select Id , Is_Closed__c from TriggerOnNotesObject__c 
                     where Is_Closed__c=True and id in :parentIds]);

   for (Note nt : trigger.new)
   {
       // if the parent id is in the map, that means the record save fails
       if (null!=parentMap.get(nt.parentId)
       { 
          nt.addError('You may not update a Note where the parent is closed');

       }
   }
}

 

All Answers

bob_buzzardbob_buzzard

I think the issue here is that you are trying to attach an error to a record that is not part of the trigger, which doesn't really make sense.  To return an error to the user you'll need to attach an error to the note that they are trying to update.

 

Off the top of my head, I think you need something closer to:

 

trigger trigrnotes on Note (before update) 
{
   Set<Id> parentIds=new Set<Id>();
   for (Note nt : trigger.new)
   {
      parentIds.add(nt.ParentId);
   }
  
   // create a map of all parent objects that are closed, keyed by id
   Map<Id, TriggerOnNotesObject__c> parentMap=new Map<Id, TriggerOnNotesObject__c>();
   parentMap.putAll([Select Id , Is_Closed__c from TriggerOnNotesObject__c 
                     where Is_Closed__c=True and id in :parentIds]);

   for (Note nt : trigger.new)
   {
       // if the parent id is in the map, that means the record save fails
       if (null!=parentMap.get(nt.parentId)
       { 
          nt.addError('You may not update a Note where the parent is closed');

       }
   }
}

 

This was selected as the best answer
Mayank_JoshiMayank_Joshi

Thanks Bob . It works ,I have to use Set and MAp for this . As under List , I won't be able use putAll , contains method . 

 

Thanks again , you made my day . 

 

 

dsprashanthdsprashanth

Can some one help me to come out of this problem???

 

trigger DisableEditButton 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');
               
      }
    }           
}

 

this is the error message i am getting after i edit and save opportunity record:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger DisableEditButton caused an unexpected exception, contact your administrator: DisableEditButton: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors: Trigger.DisableEditButton: line 14, column 1
Mayank_JoshiMayank_Joshi

Hello prashanth ,

 

It looks like ,you are getting error at line : opp.addError('You don\'t have Previlege to Edit a Renewal after status is set to Closed'); 

 

Please try to add System.debug statement as below in to your Trigger . Then check into debug logs ,how code works for  you ?I think, you need to loop and then putAll into your MAP (parentMap) . First ,try to debug as below  :

 

Map<Id, Opportunity> parentMap=new Map<Id, Opportunity>();

 

   parentMap.putAll([Select Id from Opportunity where Oppty_Status__c = 'Closed' and Id in :parentIds]);
   System.debug(    'TEST FOR MAR'+ parentMap);

 

for(Opportunity opp : Trigger.old)
    {       System.debug('TEST ONE);
            if (parentMap.get(opp.Id)!= null)
      {        System.debug('TEST TWO');
               opp.addError('You don\'t have Previlege to Edit a Renewal after status is set to Closed');
               System.debug('TEST THREE') ;
      }
    }           
}

dsprashanthdsprashanth

hello Mayank,

i  executed the code with System.debug statements and after checking with the debug logs, code was executed fine till line

 System.debug('TEST TWO'); . opp.addError() line is causing the error.

Any Idea/Help would help me a lot.

Thanks in advance