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
Amit Karlekar 4Amit Karlekar 4 

Create Related Account On Contact Insertion

Trigger Scenario:
Write a Trigger on Contact Which Will Create an Account Record Whenever Contact Is Created Without an Account.

Trigger:
trigger CreateAccountOnContactCreationTrigger on Contact (before insert, after insert) {

    if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            CreateAccountOnContactCreationHandler.immediatelyCreateAccount(Trigger.new);
        }
    }
}

Handler Class:
public class CreateAccountOnContactCreationHandler {
    
    public static void immediatelyCreateAccount(List<Contact> conList) {
        
        List<Account> accList = new List<Account>();
        
        for(Contact con : conList) {
            if(con.AccountId == null) {
                Account acc = new Account();
                acc.Name = 'Trigger Account 38';
                
                accList.add(acc);
                con.AccountId = acc.Id ;
            }
        }
        if(!accList.isEmpty()) {
            insert accList;
        }
    }
}

For the mentioned trigger scenario above, I have written the provided trigger and handler. Contact is getting created, but not the Account.

kindly tell me what caused the error? (I have even tried it with both Before and After Insert)
Piyusha PilaniaPiyusha Pilania

In the handler, you're trying to assign the AccountId from the newly created Account to the Contact. However, at that point, the Account hasn't been inserted into the database yet, so it doesn't have an Id. This means the line con.AccountId = acc.Id ; is assigning null to con.AccountId.

Updated Handler class:

public class CreateAccountOnContactCreationHandler {
    
    public static void immediatelyCreateAccount(List<Contact> conList) {
        
        List<Account> accList = new List<Account>();
        Map<Contact, Account> contactToAccountMap = new Map<Contact, Account>();
        
        for(Contact con : conList) {
            if(con.AccountId == null) {
                Account acc = new Account();
                acc.Name = 'Trigger Account 38';
                
                accList.add(acc);
                contactToAccountMap.put(con, acc);
            }
        }
        
        if(!accList.isEmpty()) {
            insert accList;

            // After insertion, update the Contact records with the Account Ids
            List<Contact> contactsToUpdate = new List<Contact>();
            for(Contact con : contactToAccountMap.keySet()) {
                con.AccountId = contactToAccountMap.get(con).Id;
                contactsToUpdate.add(con);
            }
            update contactsToUpdate;
        }
    }
}
 

 

Check this out: https://developer.salesforce.com/forums/?id=9062I000000DK2pQAG

Amit Karlekar 4Amit Karlekar 4

Thank you so much for taking some time off from your busy schedule to reply on my request.

I tried your code, as exactly as you have mentioned, and used After Insert context varible.The Contact is getting created, but the Account is not getting inserted.

It is giving me the following error:

"CreateAccountOnContactTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only Class.CreateAccountOnContactHandler.immediatelyCreateAccount: line 23, column 1 Trigger.CreateAccountOnContactTrigger: line 5, column 1"