+ Start a Discussion
Dr. Thomas MillerDr. Thomas Miller 

Add link to new child object in insert trigger

I would like to create a new child object and link it to the parent object during an insert trigger of the parent.
A similar situation is described in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm so I tried the following:

trigger TestTrigger on Account (before insert)  {
    Account acc = Trigger.new[0];
    Contact ct = new Contact(LastName = 'Smith');
        // Create the child reference.
        // Used only for foreign key reference
        // and doesn't contain any other fields.
        Contact contactReference = new Contact(MyExtID__c='SAP111111');                
        acc.primaryContact__r = contactReference;
        ct.MyExtID__c='SAP111111'; 
        insert ct;
}

Result: the contact is created but the account is not linked to the contact.

Is there a good way to do this?
(The alternative to call a future method in an after insert trigger which then links the two records is not really elegant.)
Best Answer chosen by Dr. Thomas Miller
Raj VakatiRaj Vakati
Use this code
 
trigger TestTrigger on Account (before insert)  {
    Account acc = Trigger.new[0];
    Contact ct = new Contact(LastName = 'Smith');
        // Create the child reference.
        // Used only for foreign key reference
        // and doesn't contain any other fields
		
		ct.MyExtID__c='SAP111111'; 
        insert ct;
		
		
      //  Contact contactReference = new Contact(MyExtID__c='SAP111111');                
        acc.primaryContact__c = ct.Id;
       
}

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
trigger TestTrigger on Account (before insert)  {
    Account acc = Trigger.new[0];
    Contact ct = new Contact(LastName = 'Smith');
        // Create the child reference.
        // Used only for foreign key reference
        // and doesn't contain any other fields
		
		ct.MyExtID__c='SAP111111'; 
        insert ct;
		
		
      //  Contact contactReference = new Contact(MyExtID__c='SAP111111');                
        acc.primaryContact__c = ct.Id;
       
}

 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Thomas

I hope this code will help you with your problem:

Apex Class:-

public class ChilobjectLinkToPparent 
{
   public static void LinkMethod(List<account> acctlist)
   {
       List<Contact> conlist=new List<Contact>();
       for(Account a:acctlist)
       {
           Contact c=new Contact();
               c.LastName='smith';
               c.AccountId=a.Id;
           conlist.add(c);
       }
          insert conlist;    
   }
}

Trigger:-

trigger ChilobjectLinkToPparentTrg on Account (after insert) 
{
   ChilobjectLinkToPparent.LinkMethod(trigger.new);
}

Thank you 
Ajay Dubedi
Dr. Thomas MillerDr. Thomas Miller
Thanks, Raj V,

so simple - I was too blocked in having the insert statement at the end of the method. But of course you are right: you can collect all references you need to have before the insert and add them after the insert. You do not even need the external ID stuff when you proceed like this.