You need to sign in to do that
Don't have an account?
System.Exception: SObject row does not allow errors - trying to prevent deletion
I'm looking to add an error message to an Opportunity if a user tries to delete it whilst it has any child contracts.
Currently my trigger code is as follows
trigger ClosedOpportunityPreventDeletion on Opportunity (before delete) {
if (system.Trigger.isDelete){
Opportunity[] Opps = [select id, (select id, Opportunity__c from PCFS__r) from Opportunity where id in :Trigger.oldMap.keySet()];
for (Opportunity o : Opps){
if(o.PCFS__r.size()>0){
o.adderror('You cannot delete this Opportunity as it has one or more Customer Forms associated with it');
}
}
}
}
Unfortunately, if I use this code, I currently get an error saying "caused by: System.Exception: SObject row does not allow errors".
Any ideas?
With thanks
Hi Big Ear,
You can use the addError method for only those records that are avaliable in Trigger Context.
for (Opportunity o : Opps){ if(o.PCFS__r.size()>0){ Opportunity actualRecord = Trigger.oldMap.get(o.Id); actualRecord.adderror('You cannot delete this Opportunity as it has one or more Customer Forms associated with it'); } }
All Answers
Hi Big Ear,
You can use the addError method for only those records that are avaliable in Trigger Context.
for (Opportunity o : Opps){ if(o.PCFS__r.size()>0){ Opportunity actualRecord = Trigger.oldMap.get(o.Id); actualRecord.adderror('You cannot delete this Opportunity as it has one or more Customer Forms associated with it'); } }
Hi Big Ear,
You can use the addError method for only those records that are avaliable in Trigger Context.
for (Opportunity o : Opps){ if(o.PCFS__r.size()>0) { Opportunity actualRecord = Trigger.oldMap.get(o.Id); actualRecord.adderror('You cannot delete this Opportunity as it has'); } }
Excellent. Thank you. :)
It Actually works. Thanks.
This doesn seem to work . Can someone help !
This is what i am doing..
if(Trigger.isUpdate){
List<Lead> oldlist=[Select id,name,email from lead where id IN :trigger.oldmap.keyset()];
system.debug('oldleadlist' +oldlist);
for(lead ll:oldlist){
system.debug('checking for loop');
if(ll.email==null){
system.debug('check if loop');
ll.email='updatedemail@gmail.com';
//update ll;
}
else
{
List<contact> conlist2 =[select id,name,email from contact where email =:ll.email];
if(conlist2.size()>0)
{
Lead lTwo= trigger.oldmap.get(ll.id);
system.debug('check lead id' +lTwo);
lTwo.adderror('dup email on update');
}
}
"Lead lTwo= trigger.oldmap.get(ll.id);" to:
"Lead lTwo= trigger.newmap.get(ll.id);"
The documentation states: "When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged."
So, based on the DML operation happening, you'll need to select the correct one out of oldMap and newMap.
Andy