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 new record and populate fields.

I want to create a relationship between two contacts and have the relationship listed in the same related list on both contacts.

I created a relationship junction record using 2 lookup fields to the contact object. When I create the record it wont display the relation in the same related list on each object. In order to do that, upon creating the relationship record I need apex to duplicate the record and at the same time insert the same fields for the lookup but just switch them around, contact "A" and contact "B" should switch positions on the duplicate.

I tried writing some apex put im having trouble withe the creation of a new record, as opposed to an update on the current one.

trigger NewRelationship 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();
        NewRec.First_person_in_relationship__c = x.Second_person_in_relationship__c;
        NewRec.First_person_is_second_persons__c = x.Second_Person_is_first_persons__c;
        NewRec.Second_person_in_relationship__c = x.First_person_in_relationship__c;
        NewRec.Second_Person_is_first_persons__c = x.First_person_is_second_persons__c;
        newRelationship.add(NewRec);
   }
   INSERT newRelationship;
}

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger NewRelationship caused an unexpected exception, contact your administrator: NewRelationship: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewRelationship: maximum trigger depth exceeded Relationship trigger event AfterInsert for [a002000000VpusW] Relationship trigger event AfterInsert for [a002000000VpusX] Relationship trigger event AfterInsert for [a002000000VpusY] Relationship trigger event AfterInsert for [a002000000VpusZ] Relationship trigger event AfterInsert for [a002000000Vpusa] Relationship trigger event AfterInsert for [a002000000Vpusb] Relationship trigger event AfterInsert for [a002000000Vpusc] Relationship trigger event AfterInsert for [a002000000Vpusd] Relationship trigger event AfterInsert for [a002000000Vpuse] Relationship trigger event AfterInsert for [a002000000Vpusf] Relationship trigger event AfterInsert for [a002000000Vpusg] Relationship trigger event AfterInsert for [a002000000Vpush] Relationship trigger event AfterInsert for [a002000000Vpusi] Relationship trigger event AfterInsert for [a002000000Vpusj] Relationship trigger event AfterInsert for [a002000000Vpusk] Relationship trigger event AfterInsert for [a002000000Vpusl]: []: Trigger.NewRelationship: line 18, column 1
Slava RamSlava Ram
You have run an infinite cercle of create - insert - trigger create - insert - trigger create - ets. So the systen run out of governor dml limits and recomended to buy a greater licence.
Tzemach Aronow 4Tzemach Aronow 4
Can you please show me how to stop the circle
Vinit_KumarVinit_Kumar
Not sure what you are trying to do,it seems you are trying to insert a duplicate record when a new record is inserted,this would cause recursive firing of Trigger.Try below approach this should work :-

Create an Apex class in your org like below :- 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Now,use this class to stop Trigger recursion,something like below :-

trigger NewRelationship on Relationship__c (before insert) {
    List<relationship__c> NewRelationship = new List <relationship__c> ();

    for (relationship__c x : Trigger.new) {
	     if(checkRecursive.runOnce())
        relationship__c NewRec = new relationship__c();
        NewRec.First_person_in_relationship__c = x.Second_person_in_relationship__c;
        NewRec.First_person_is_second_persons__c = x.Second_Person_is_first_persons__c;
        NewRec.Second_person_in_relationship__c = x.First_person_in_relationship__c;
        NewRec.Second_Person_is_first_persons__c = x.First_person_is_second_persons__c;
        newRelationship.add(NewRec);
		}
   }
   if(newRelationship.size()>0)
   {
	INSERT newRelationship;
   }	
}

If this helps,please mark it as best answer to help others :)