You need to sign in to do that
Don't have an account?
sumit d
creating reverse records in trigger for junction object
Hi All,
I have a custom object called Family which is a junction object between two accounts. Below is the schema of the object.
Object Name: FBG_Family__c Custom fields:
1) (Sister_Company__c)--lookup relationship
2) (Sister_Company2__c)-- lookup relationship
lets assume we have two accounts,
Account A, Account B. Lets create an FBG_Family__c records between these two. Every time a FBG_Family__c record is inserted, we have to create another FBG_Family__c record and have to swap the Sister_Company__c and Sister_Company2__c Ids in the new record.
i have created a trigger for it which 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(trigger.new);
}
}
}
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(List<FBG_Family__c> FBGFamilyIds){
System.debug('processed'+FBGFamilyIds);
List<FBG_Family__c> ListFamilyToInsert = New List<FBG_Family__c>();
For(FBG_Family__c Family : FBGFamilyIds)
{
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){
Insert ListFamilyToInsert;
}
}
}
when i create a record its giving me errror:-Trigger_FBGFamily: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_FBGFamily: maximum trigger depth exceeded FBG_Family trigger event AfterInsert
how to solve this error?
i tried implementing variable to stop recursion too but no luck.
Am i missing something in the code?
Any suggestions?
I have a custom object called Family which is a junction object between two accounts. Below is the schema of the object.
Object Name: FBG_Family__c Custom fields:
1) (Sister_Company__c)--lookup relationship
2) (Sister_Company2__c)-- lookup relationship
lets assume we have two accounts,
Account A, Account B. Lets create an FBG_Family__c records between these two. Every time a FBG_Family__c record is inserted, we have to create another FBG_Family__c record and have to swap the Sister_Company__c and Sister_Company2__c Ids in the new record.
i have created a trigger for it which 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(trigger.new);
}
}
}
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(List<FBG_Family__c> FBGFamilyIds){
System.debug('processed'+FBGFamilyIds);
List<FBG_Family__c> ListFamilyToInsert = New List<FBG_Family__c>();
For(FBG_Family__c Family : FBGFamilyIds)
{
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){
Insert ListFamilyToInsert;
}
}
}
when i create a record its giving me errror:-Trigger_FBGFamily: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_FBGFamily: maximum trigger depth exceeded FBG_Family trigger event AfterInsert
how to solve this error?
i tried implementing variable to stop recursion too but no luck.
Am i missing something in the code?
Any suggestions?
I see that the recursion is causing this error. Did you try this instead of variable?
http://sfdcsrini.blogspot.com/2014/06/what-is-maximum-trigger-depth-exceeded.html
Please mark it as the best answer if this helps.
Cheers!
Sathish