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
Big EarsBig Ears 

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 

 

 

Message Edited by Big Ears on 07-28-2009 06:54 AM
Best Answer chosen by Admin (Salesforce Developers) 
ShamSham

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

ShamSham

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

 

 

This was selected as the best answer
ShamSham

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

 

 

 

 

Big EarsBig Ears

Excellent. Thank you. :)

FireFire

It Actually works. Thanks.

swathi krishnamurthyswathi krishnamurthy
Hi , 

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');
             }
             }
Big EarsBig Ears
I think you need to change

"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
Big EarsBig Ears
It's also worth mentioning that it looks like you've got a SOQL query in a loop here. I think it would be better to take that outside the loop in a way similar to this:
if(Trigger.isUpdate){
	List<Lead> oldlist=[Select id,name,email from lead where id IN :trigger.oldmap.keyset()];
	
	//list<string> conEmailList = new list<string>
	map<string, email> leadEmailMap = new map<string, email>();
	
	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 {
			//conEmailList.add(ll.Email);
			leadEmailMap.put(ll.Email, ll);
		}
	}
	
	if(conEmailList.size() > 0){
		List<contact> conlist2 =[select id,name,email from contact where email IN :leadEmailMap.keySet()];
		
		for(Contact c : conlist2){
			Lead lTwo = trigger.newMap.get(leadEmailMap.get(c.email).id);
			lTwo.adderror('dup email on update');
		}
	}
}