+ Start a Discussion
HARSHIL U PARIKHHARSHIL U PARIKH 

Trigger does not allow deletion of record

Hello Developers,
Need your thoughts/help..
I have a trigger on child object which updates check box on contact as True on creation of child. (This happens in below trigger)
But it doesn't allow me to delete the child for that contact anymore.
Trigger CrossObjectContactUpdate on Merchandise__C(after delete, after insert, after update){


    List<Merchandise__c> Merch = New List<Merchandise__C>();
    List<Contact> Con = New List<Contact>();
    Set<ID> ConID = New Set<ID>();
        
    for(Merchandise__c M: Trigger.New)
    {
       ConId.add(M.Individual_Customer__C);
    }
    Boolean T = True;
    
    Contact Con1 = [Select X_CrossObj__c From Contact Where ID IN: ConID];
    for(Merchandise__C MC: Trigger.New)
    {    
        Con1.X_CrossObj__c = T;
    }
    update Con1;
    
}

This trigger is created to just update the record of parent. So my other triggers can fire. So, having checkbox checked or not checked don't really matter. Its all about update.
Thank You!

Well, one more Q) Can we update parent record (just update) when child is created, deleted, updated etc.. with process builder? IF yes, then I can take that path instead of trigger above.
Thank you guys for help!
 
Best Answer chosen by HARSHIL U PARIKH
Art Smorodin 7Art Smorodin 7
Try this, see if it does the trick. I basically recolased  Contact Con1, which is a single record with a List of Contacts
Trigger CrossObjectContactUpdate on Merchandise__C(after delete, after insert, after update){

	List<Merchandise__c> Merch = New List<Merchandise__C>();
    List<Contact> Con = New List<Contact>();
    Set<ID> ConID = New Set<ID>();
    if (!Trigger.isDelete){ //Trigger.New will not exist if record is being deleted
		for(Merchandise__c M: Trigger.New){
			ConId.add(M.Individual_Customer__C);
		}
	}
	else {	//for deleted records use Trigger.old
		for(Merchandise__c M: Trigger.Old){
			ConId.add(M.Individual_Customer__C);
		}
	}
    Boolean T = True;
    
    //Contact Con1 = [Select X_CrossObj__c From Contact Where ID IN: ConID];
	List <Contact> Con1 = new List<Contact>([Select X_CrossObj__c From Contact Where ID IN: ConID]); //instead of having a single record we now have a list
	if (!Trigger.isDelete){
		for(Merchandise__C MC: Trigger.New){   
			for (Contact ct: Con1){ // loop through the list and update fields 
				ct.X_CrossObj__c = T;
			}
		}
	}
	else {
		for(Merchandise__C MC: Trigger.Old){    
			for (Contact ct: Con1){ // loop through the list and update fields 
				ct.X_CrossObj__c = T;
			}
		}
	}
    
	update Con1;
}

-Art

All Answers

Art Smorodin 7Art Smorodin 7
Hi,
I think the problem is with Trigger.New. Basically when you delete the record  Trigger.New will no longer exist. 

You have two options here. 1) simply take out 'after delete' from your trigger declaration. Sp, you forst line will look like: Trigger CrossObjectContactUpdate on Merchandise__C(after insert, after update).

2) if you want to keep  'after delete' in your trigger declaration:
Trigger CrossObjectContactUpdate on Merchandise__C(after delete, after insert, after update){


    List<Merchandise__c> Merch = New List<Merchandise__C>();
    List<Contact> Con = New List<Contact>();
    Set<ID> ConID = New Set<ID>();
    if (!Trigger.isDelete){ //Trigger.New will not exist if record is being deleted
		for(Merchandise__c M: Trigger.New){
			ConId.add(M.Individual_Customer__C);
		}
	}
	else {	//for deleted records use Trigger.old
		for(Merchandise__c M: Trigger.Old){
			ConId.add(M.Individual_Customer__C);
		}
	}
    Boolean T = True;
    
    Contact Con1 = [Select X_CrossObj__c From Contact Where ID IN: ConID];
	if (!Trigger.isDelete){
		for(Merchandise__C MC: Trigger.New){    
			Con1.X_CrossObj__c = T;
		}
	}
	else {
		for(Merchandise__C MC: Trigger.Old){    
			Con1.X_CrossObj__c = T;
		}
	}
    
	update Con1;
    
}

Hope this helps.
Art.
HARSHIL U PARIKHHARSHIL U PARIKH
Awesome! Trigger works fine accept one condition.
When I do mass update (using installed app for Mass edit/update/delete from list view) of the record then it throws an error like this:
Error:
CrossObjectContactUpdate: execution of AfterUpdate caused by: System.QueryException: List has more than 1 row for assignment to SObject () No record updated
Thank you for the followup!
 
Art Smorodin 7Art Smorodin 7
Try this, see if it does the trick. I basically recolased  Contact Con1, which is a single record with a List of Contacts
Trigger CrossObjectContactUpdate on Merchandise__C(after delete, after insert, after update){

	List<Merchandise__c> Merch = New List<Merchandise__C>();
    List<Contact> Con = New List<Contact>();
    Set<ID> ConID = New Set<ID>();
    if (!Trigger.isDelete){ //Trigger.New will not exist if record is being deleted
		for(Merchandise__c M: Trigger.New){
			ConId.add(M.Individual_Customer__C);
		}
	}
	else {	//for deleted records use Trigger.old
		for(Merchandise__c M: Trigger.Old){
			ConId.add(M.Individual_Customer__C);
		}
	}
    Boolean T = True;
    
    //Contact Con1 = [Select X_CrossObj__c From Contact Where ID IN: ConID];
	List <Contact> Con1 = new List<Contact>([Select X_CrossObj__c From Contact Where ID IN: ConID]); //instead of having a single record we now have a list
	if (!Trigger.isDelete){
		for(Merchandise__C MC: Trigger.New){   
			for (Contact ct: Con1){ // loop through the list and update fields 
				ct.X_CrossObj__c = T;
			}
		}
	}
	else {
		for(Merchandise__C MC: Trigger.Old){    
			for (Contact ct: Con1){ // loop through the list and update fields 
				ct.X_CrossObj__c = T;
			}
		}
	}
    
	update Con1;
}

-Art
This was selected as the best answer