Update related Account field on creating new Contact

Update related Account on creating new contact
Update related Account on creating new contact

Scenario – We want to update selected Account field when new Contact is created.

Solution :

  1. Using Trigger
  2. Using Record Trigger Flow

In this article you will learn how to solve the given scenario using Trigger. You can also write Record Trigger flow for the same.

Create After Insert Trigger on Contact

				
					trigger ContactTrigger on Contact (after insert) {
    
    ContactTriggerHandler.updateAccount(Trigger.new);

}
				
			

Creating Trigger Handler

				
					public class ContactTriggerHandler {
    public static void updateAccount(List<Contact> record) {
        Contact contactRecord = [SELECT Account.Name from Contact where Id In: record];

        contactRecord.Account.Name = 'John Doe';
        update contactRecord.Account; 
    }
}
				
			

In the above code we are assuming that we have only record to process . Let’s see how to handle the same for bulk record.

Handling Bulk Records

				
					public static void updateAccount(List<Contact> records) {
        List<Contact> lstContact = [SELECT Account.Name from Contact where Id In: records];
        
        for(Contact con : lstContact) {
            con.Account.Name = 'Awasome';
            update con;
        }
}
				
			

This is how we can update related Account field when new Contact is created , you can use the same to update any related object records.

Problems in both versions

  • The first handler stores a query result in one Contact variable. With more than one new Contact, the query returns a list and the code fails.
  • Both versions change Account.Name through the Contact relationship. A change made on the related record this way is not saved. You must update the Account record itself.
  • The bulk version runs update con inside the loop, which breaks the DML limit and updates the Contact instead of the Account.

Correct bulk version

public class ContactTriggerHandler {
    public static void updateAccount(List<Contact> records) {
        Set<Id> accountIds = new Set<Id>();
        for (Contact con : records) {
            if (con.AccountId != null) {
                accountIds.add(con.AccountId);
            }
        }
        List<Account> accountsToUpdate = new List<Account>();
        for (Id accId : accountIds) {
            // replace Description with the field you want to change
            accountsToUpdate.add(new Account(Id = accId, Description = 'Contact added on ' + System.today()));
        }
        if (!accountsToUpdate.isEmpty()) {
            update accountsToUpdate;
        }
    }
}

The Set removes duplicate Account Ids. If you add two Contacts to the same Account in one insert, a list with the same Id twice would fail with a Duplicate id in list error.

How to test

  1. Create one Contact on an Account and check that the field changed.
  2. Insert two Contacts for the same Account in one Data Loader file. The update should work without an error.
  3. Create a Contact with no Account. The trigger should not fail.

FAQ

Why after insert?
The Contact does not need changes. We update a different record, so after insert is fine.

Is Flow better for this?
For a single field update, a record-triggered Flow is simpler to maintain.

Leave a Reply