You need to sign in to do that
Don't have an account?
Tzemach 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.
I also tried
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; }
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.
Thank you for your help
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
is this better? it allowes me to save but when i create the record i still get an error.
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