• Erika REILLIER
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
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 !
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 !