+ Start a Discussion
sumit suwalkasumit suwalka 

update and delete records when change in another records

Hi All,
         i have a requirement in which i created a trigger when a record is created. i am creating reverse records of same object. now i want that if record is updated or deleted it should update and delete the reverse record too.
my trigger and helper is given below:-
trigger Trigger_FBGFamily on FBG_Family__c (before insert, before update, 
                                            before delete, after insert, 
                                            after update, after delete) {
                                                
    FBGFamilyTriggerHelper.newFBGFamily = trigger.new;
    FBGFamilyTriggerHelper.oldFBGFamily = trigger.old;
    FBGFamilyTriggerHelper.newMapFBGFamily = trigger.newMap;
    FBGFamilyTriggerHelper.oldMapFBGFamily = trigger.oldMap;   
                                                
       if(!FBGFamilyTriggerHelper.runTrigger) {
        return;
    }
                                                if( Trigger.isAfter ){
                                                    if(Trigger.isInsert ){
                                                        FBGFamilyTriggerHelper.CreatingReverseFamilyRecords();   
                                                    }
                                                    
                                                }
                                            }
my helper is given below:-
public class FBGFamilyTriggerHelper {
    public static List<FBG_Family__c> newFBGFamily = new List<FBG_Family__c>();
    public static List<FBG_Family__c> oldFBGFamily = new List<FBG_Family__c>();
    public static Map<Id, FBG_Family__c> newMapFBGFamily = new Map<Id, FBG_Family__c>();
    public static Map<Id, FBG_Family__c> oldMapFBGFamily = new Map<Id, FBG_Family__c>();
    
        public static boolean runTrigger = TRUE;
    
     Public static void CreatingReverseFamilyRecords(){
        System.debug('processed'+newFBGFamily);
        List<FBG_Family__c> ListFamilyToInsert = New List<FBG_Family__c>();
         For(FBG_Family__c Family : newFBGFamily)
        {
            FBG_Family__c ReverseFamily = New FBG_Family__c();
            ReverseFamily.Sister_Company__c = Family.Sister_Company2__c;
            ReverseFamily.Sister_Company2__c = Family.Sister_Company__c;
            ListFamilyToInsert.Add(ReverseFamily);
            System.debug('ReverseFamily'+ListFamilyToInsert);

         }
        if(ListFamilyToInsert.Size() > 0){
            runTrigger = FALSE;
          Insert ListFamilyToInsert;
            runTrigger = TRUE;
           System.debug('ReverseFamily'+ListFamilyToInsert);

         }
    }
}
how to do it ? any suggestions?