+ Start a Discussion
sumit suwalkasumit suwalka 

SELF_REFERENCE_FROM_TRIGGER, therefore it cannot recursively delete itself: []

Hi All,
       i have a trigger in which i am inserting reverse records.i have imple mented the trigger its working fine for insertion . now i want that if i update or delete a record its reverse record should also get updated or deleted.
       my trigger 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();   
                                                    }
                                                    
                                                }
                                                 if( Trigger.isBefore ){
                                                    if(Trigger.isDelete ){
                                                        FBGFamilyTriggerHelper.deletingReverseFamilyRecords();   
                                                    }
                                                    
                                                }
                                            }
        my helper class 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);

         }
    }
    public static void deletingReverseFamilyRecords(){
         //To store Family ids
    list<id> Famids = new list<id>();
    for(FBG_Family__c Family : oldFBGFamily)
    {
        system.debug('old list'+oldFBGFamily);
        Famids.add(Family.id);
    }  
    system.debug('Famids'+Famids);

    //Collecting all records related to Family records
        list<FBG_Family__c> listOfFamilyToDelete = [SELECT id  
                                                    FROM FBG_Family__c 
                                                    WHERE ID IN : Famids
                                                   ];
    system.debug('listOfFamily'+listOfFamilyToDelete);
    //deleting Reverse records
     if(listOfFamilyToDelete.Size() > 0){
     delete listOfFamilyToDelete;
      }  
    }
}
its not working for deletion.
Any suggestion? how to solve it?
Lokesh KumarLokesh Kumar
change to after delete. 
sumit suwalkasumit suwalka
Hi lokesh, 
it didnt worked out on after delete.
Any suggestions?
Lokesh KumarLokesh Kumar
I see in the Code you are deleting the same record through trigger which you have already deleted from the delete button 
sumit suwalkasumit suwalka
yes, i want to delete another reverse record which is created after insertion on record.
what should i change in code?
Lokesh KumarLokesh Kumar
can you please write down the use case which you want to implement
sumit suwalkasumit suwalka
in first method i am inserting a reverse record on insertion on same object via trigger. where   .Sister_Company__c = Sister_Company2__c; and
            .Sister_Company2__c =  Sister_Company__c;
so i want that if i delete first record its reverse record also get deleted