Create Parent Record from Child record in Before Trigger

In this example, we will create Account record from a Contact record and associate it with it in before insert. In a general scenario what one would do is

1. Create a child record.
2. Create its Parent record.
3. Fill the Parent Record Id in child record and update it.

this scenario will cause 3 DML operation, one for each case.
We will be optimizing this with a workaround to create Parent Record before a child record is committed to Database and fill its parent Id.
What we will do is
1. Create a Parent Record
2. Before committing to Database update the parent record Id field in its child record.

ContactTrigger

trigger ContactTrigger on Contact (before insert) {
  if(Trigger.isBefore && Trigger.isInsert) {
    ContactTriggerHandler.beforeInsert(Trigger.new);
  }
}

ContactTriggerHandler

public class ContactTriggerHandler {  
  //Before Insert Logic
  public static void beforeInsert(List<Contact> newList) {
    
    //List to hold contact for which new account is created
    List<Contact> contactwithNewAccountList = new List<Contact>();
    
    //List to hold new Account List
    List<Account> newAccountList = new List<Account>();
    
    for(Contact con: newList) {
      //Check if AccountId is empty
      if(con.AccountId == null) {
        //Create Account
        Account acc = new Account();
        acc.Name = con.LastName;
        acc.OwnerId = con.OwnerId;
        
        newAccountList.add(acc);
        contactwithNewAccountList.add(con);
      }
    }
    
    //Insert Account Records
    if(!newAccountList.isEmpty()) {
      insert newAccountList;
    }
    
    //Fill Account Record's Id in Contact's AccountId Field  
    for(integer i = 0; i < newAccountList.size(); i++ ) {
      contactwithNewAccountList.get(i).AccountId = newAccountList.get(i).Id;
    }
  }
}

As you have seen that we have created Parent Record before child record was saved in Database and it saved us from making additional DML for child record Update.

We here have utilized the property of the object that we aren’t actually using objects when you assign an object to a variable or pass an object to a method as an argument. Instead, we’re using references to those objects. In our case, we have created a new list contactwithNewAccountList for all those contacts which will have a new account. We will update the AccountId field in that list and due to the property of object mentioned above, it will update the Trigger.New as well.

Let us know if you have you find this blog helpful or if you have any suggestions.

Was this post useful? If yes then please share.



Comments (4)

  1. Josh Moore says:

    Hello!

    I am putting together an article detailing the top apps for new parents. I thought it would be perfect for your readers, and I’d love to send it to you — free of charge — when it’s finished. Would you be interested in posting it?

    Please just let me know either way!
    Josh

    Josh Moore
    Diaperdads.org
    [email protected]

  2. valli says:

    This medhod how to write the test class, please send answer very argent.

Leave a Comment

All fields marked with an asterisk (*) are required


shares