You need to sign in to do that
Don't have an account?
Erika 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 !
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 !
Please try the below code and let me know if it works as expected.
My A object is the standard Contact object, maybe it is related ?
List is expecting an object, not an Id.
change to
or change
to
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 :(
Incorrect
Correct