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
indranily81indranily81 

adderror() throwing exception

Hi,

I have written a before trigger  but I am getting the exception.I have two objects as Opp__c and Budget__c.
There are master details relationships between these two.
 

Exception :

 
deletebudget: execution of BeforeDelete
caused by: System.Exception: SObject row does not allow errors

 

Code :-

trigger deletebudget on Opp__c (before delete) {
   for(Opp__c lopp:Trigger.old){
           
            Opp__c l_opp = [Select o.Budget__c, o.Id from Opp__c o where o.Id = :Trigger.old[0].Id];
            Budget__c l_bud = [Select b.Id, b.Name, b.Total_budget__c from Budget__c b where b.Id = :l_opp.Budget__c];
                    
            if(l_bud.Total_budget__c <= 0 ) {
                if(Trigger.isDelete) {             
                   System.debug('CORRECT FOR LESS THAN 0');
                   l_bud.addError('Got an error from trigger while deleting');
              }
            }
           
             if(l_bud.Total_budget__c > 0 ) {
                if(Trigger.isDelete) {             
                  System.debug('deletion successful');
                  
                }
            }
              
       }

}
Best Answer chosen by Admin (Salesforce Developers) 
indranily81indranily81

Got the solution.

 

It should beTrigger.old[0].addError('Got an error from trigger while deleting'). Just posting it if it helps any newbie.

All Answers

indranily81indranily81

My test class was able to print upto 'CORRECT FOR LESS THAN 0' message but no exception is thrown.

My intention is to show an error message if the deletion is failed. Any idea would be of great help.

indranily81indranily81

Got the solution.

 

It should beTrigger.old[0].addError('Got an error from trigger while deleting'). Just posting it if it helps any newbie.

This was selected as the best answer