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> RecToInsert = new List <relationship__c> ();

for (relationship__c x : Trigger.new) {

    x.First_person_in_relationship__c = x.Second_person_in_relationship__c;
    x.First_person_is_second_persons__c = x.Second_Person_is_first_persons__c;
    x.Second_person_in_relationship__c = x.First_person_in_relationship__c;
    x.Second_Person_is_first_persons__c = x.First_person_is_second_persons__c;

    RecToInsert.add(x);

    try{

    insert RecToInsert;
    } catch (system.Dmlexception e) {
    system.debug (e);
    }

}
}



I also tried


trigger NewRelationship on Relationship__c (after insert) {

    List<relationship__c> RecToInsert = new List <relationship__c> ();

    for (relationship__c x : Trigger.new) {
        
                        RecToInsert.add(
                                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)
   
  ); }
        INSERT RecToInsert;
       
   

    
}

Tzemach Aronow 4Tzemach Aronow 4
Another version I tried

trigger NewRelationship on Relationship__c (after insert) {

    

    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;
        INSERT NewRec;
   }
}
EguiEgui
Hi,

It's not best practice to do insert in a loop.

It's recommended to add records to a list and to insert it after the loop.
Tzemach Aronow 4Tzemach Aronow 4
Can you please explain, im new and not to familiar.
Thank you for your help
EguiEgui
Sure, as Salesforce is executing in the cloud they are some limits to respect in order to maintain good system perfomance.

If you do in insert in a loop, it will consume a statement each time it loops. If you store your records in a list and insert it at the end outside of the loop it will consume only one statement.

You shoud give a look at this article : https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Tzemach Aronow 4Tzemach Aronow 4
Thanks

is this better? it allowes me to save but when i create the record i still get an error.

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