function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tzemach Aronow 4Tzemach Aronow 4 

Trigger to create 2nd record when creating a record within the same object

Im trying to create a trigger simmilar to the npsp relationship pack, that when I create a reationship record on contact "A", it should create a duplicate relationship on the record of contact "B".

I'm new to SF and I have tried a few ways but I'm still having trouble with my code.

trigger NewRelationships on Relationship__c (after insert) {

    List<relationship__c> NewRelationship = new List <relationship__c> {};

    for (relationship__c x : Trigger.new) {
        
        
           
        relationship__c NewRec = new relationship__c(
        First_person_in_relationship__c = x.Second_person_in_relationship__c,
        First_person_is_second_persons__c = x.Second_Person_is_first_persons__c,
        Second_person_in_relationship__c = x.First_person_in_relationship__c,
        Second_Person_is_first_persons__c = x.First_person_is_second_persons__c);
        NewRelationship.add(x);
        }
        
    
    
       insert NewRelationship;
      
}


Chidambar ReddyChidambar Reddy
Hi,

It is becuase of recursion


When you insert newRelationship records from Trigger, The trigger will again call after insertion.


You must stop this recursion using a static variable


Create a class
Public class RecursionHandler{
      public static boolean runOnce = TRUE;

}

and use this static variable in the Trigger

trigger NewRelationships on Relationship__c (after insert) {

  if(RecursionHandler.runOnce){
    List<relationship__c> NewRelationship = new List <relationship__c> {};

    for (relationship__c x : Trigger.new) {
        
        
           
        relationship__c NewRec = new relationship__c(
        First_person_in_relationship__c = x.Second_person_in_relationship__c,
        First_person_is_second_persons__c = x.Second_Person_is_first_persons__c,
        Second_person_in_relationship__c = x.First_person_in_relationship__c,
        Second_Person_is_first_persons__c = x.First_person_is_second_persons__c);
        NewRelationship.add(x);
        }
        
    
    
       insert NewRelationship;
      
      RecursionHandler.runOnce = FALSE;
   }//end of If
}


Choose it as Best Answer if it resolves your issue
Chidambar ReddyChidambar Reddy
Please ignore those <strong> text in the trigger


trigger NewRelationships on Relationship__c (after insert) {

  if(RecursionHandler.runOnce){
    List<relationship__c> NewRelationship = new List <relationship__c> {};

    for (relationship__c x : Trigger.new) {
        
        
           
        relationship__c NewRec = new relationship__c(
        First_person_in_relationship__c = x.Second_person_in_relationship__c,
        First_person_is_second_persons__c = x.Second_Person_is_first_persons__c,
        Second_person_in_relationship__c = x.First_person_in_relationship__c,
        Second_Person_is_first_persons__c = x.First_person_is_second_persons__c);
        NewRelationship.add(x);
        }
        
    
    
       insert NewRelationship;
      
     RecursionHandler.runOnce = FALSE;
   }//end of If
}