You need to sign in to do that
Don't have an account?
indranily81
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');
}
}
}
}
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');
}
}
}
}
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
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.
Got the solution.
It should beTrigger.old[0].addError('Got an error from trigger while deleting'). Just posting it if it helps any newbie.