+ Start a Discussion
Erika REILLIERErika REILLIER 

Trigger to backup child record from the master

Hi Community, 
I'm making my very first steps in triggers and am trying to set up a trigger to save child record data despite of the cascade deletion in master-detail relationship : if a record from the A master object is deleted, if it has any B detail records, those B detail records should be backup in a C record.
I already created a trigger if B is deleted but now can't get the additional trigger on A.
Here is what I did but get an error when trying to get data of the B child records, I must be missing something between the two 'List" ?

trigger BackupAIfAssociated on A (before delete) {
for (A a:trigger.new)
{
    list< B> c = [select B lookup from B where  B lookup=:a.id];
    if (c.size()>0){
    
    List<C> lstToInsrt = new List<C__c>();  
 for(B deletedB : trigger.old)
        {          
        C__c obj = new C__c();
              obj = new C__c();
              obj.B.Name = deletedB.Name

            lstToInsrt.add(obj);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
        }
}

}

Thank you very much for your help !
Anant KamatAnant Kamat
Hello Erika,
Please try the below code and let me know if it works as expected.
 
trigger BackupAIfAssociated on A (before delete) {
	List<A> aIds = new List<A>();

	for (A a : trigger.new){
		aIds.add(a.Id);
	}
	
	List<B> lstB = [select lookup__c from B where lookup__c IN :aIds];
	List<C> lstToInsrt = new List<C__c>();  
	
	if (!lstB.isEmpty()){
		for (B b: lstB)
		{
			C__c obj = new C__c();
			obj.Name = b.Name;
			lstToInsrt.add(obj);
        }
        if(!lstToInsrt.isEmpty()){
            insert lstToInsrt;
        }
    }
}

 
Erika REILLIERErika REILLIER
Thank you for your fast reply ! I tried and get a compile error on the add(Id) on aIds.add(a.Id)
My A object is the standard Contact object, maybe it is related ? 
Tad Aalgaard 3Tad Aalgaard 3
That's because in Anants example there is a bug. 

List is expecting an object, not an Id.

change 
List<A> aIds = new List<A>();
to
List<Id> aIds = new List<Id>();

or change
aIds.add(a.Id);

to
aIds.add(a);


 
Erika REILLIERErika REILLIER
Thanks Tad, second option helped for this !
However now I get a compile error: Invalid loop variable type expected A was B at line 12 ;
I tried with For(A lstC : trigger.old), get no error, but won't get my B info backed up in C when A is deleted :(
 
 
 
 
Erika REILLIERErika REILLIER
Actually if I do this, I can't retrieve B info in C (variable does not exit)
 
 
 
 
Tad Aalgaard 3Tad Aalgaard 3
When looping through items the item type in the list must match the item you are extracting out.

Incorrect
// trigger.old is a list of A objects

 // fails because you are trying to put an item from the trigger.old list of items of type A into a variable of type C.
For(C item : trigger.old)


Correct
// trigger.old is a list of A objects

 // works because you are trying to put an item from the trigger.old list of items of type A into a variable of type A.
For(A item : trigger.old)

 
Erika REILLIERErika REILLIER
Understood, many thanks for your time and help !