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
Priyank SahuPriyank Sahu 

Hi guys, I want to write a trigger on account to update contact fields.

 Actually the condition is that i have department field picklist in Account, whenever i change the department(picklist) from account  it should be reflect to the department field in contact. I have to write a trigger to update the records in contact.  
Best Answer chosen by Priyank Sahu
Glyn Anderson 3Glyn Anderson 3
This code should be used in an "after update" trigger on Account.  (Disclaimer:  This code has not been tested and may have typos.)

<pre>
// figure out which accounts have changed department
Set<Id> accountIdsWithNewDepartment = new Set<Id>();
for ( Account account : Trigger.new )
{
    Account oldAccount = Trigger.oldMap.get( account.Id );
    if ( newAccount.Department__c != oldAccount.Department__c )
    {
        accountIdsWithNewDepartment.add( account.Id );
    }
}

// update the contacts on the affected accounts
if ( !accountIdsWithNewDepartment.isEmpty() )
{
    List<Contact> contactsToUpdate = 
        [   SELECT  Id, Department__c, AccountId
            FROM    Contact
            WHERE   AccountId IN :accountIdsWithNewDepartment
        ];
    for ( Contact contact : contactsToUpdate )
    {
        contact.Department__c = Trigger.newMap().get( AccountId ).Department__c
    }
    update contactsToUpdate;
}
</pre>

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Priyank sahu,

Trigger on Account to update Contact may I suggest you please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Srinivasa Chary TaduriSrinivasa Chary Taduri
I have just written a method below. Keep this method in a class and invoke from  after update trigger (Trigger on Account) and pass the trigger.new.

Correct the Department__c field api name in the below code.


public static void populateDepOnContact(List<Account> vLstAccount)
{
    List<Contact> vLstContact = [Select Id, AccountId, Department__c from Contact where AccountId In: vLstAccount];
    
    for(Account vAccount: vLstAccount)
    {
        for(Contact vContact: vLstContact)
        {
            if(vAccount.Id == vContact.AccountId)
            {
                vContact.Department__c = vAccount.Department__c;
            }
        }
    }
    
    if(!vLstContact.isEmpty())
    {
        update vLstContact;
    }
    
}
Srinivasa Chary TaduriSrinivasa Chary Taduri
Please mark it as correct
Glyn Anderson 3Glyn Anderson 3
This code should be used in an "after update" trigger on Account.  (Disclaimer:  This code has not been tested and may have typos.)

<pre>
// figure out which accounts have changed department
Set<Id> accountIdsWithNewDepartment = new Set<Id>();
for ( Account account : Trigger.new )
{
    Account oldAccount = Trigger.oldMap.get( account.Id );
    if ( newAccount.Department__c != oldAccount.Department__c )
    {
        accountIdsWithNewDepartment.add( account.Id );
    }
}

// update the contacts on the affected accounts
if ( !accountIdsWithNewDepartment.isEmpty() )
{
    List<Contact> contactsToUpdate = 
        [   SELECT  Id, Department__c, AccountId
            FROM    Contact
            WHERE   AccountId IN :accountIdsWithNewDepartment
        ];
    for ( Contact contact : contactsToUpdate )
    {
        contact.Department__c = Trigger.newMap().get( AccountId ).Department__c
    }
    update contactsToUpdate;
}
</pre>
This was selected as the best answer
Glyn Anderson 3Glyn Anderson 3
There's a typo on line 22 of my code above.  It should be:

<pre>
contact.Department__c = Trigger.newMap().get( contact.AccountId ).Department__c
</pre>
Priyank SahuPriyank Sahu
Thanks Glyn Anderson for your suggestion. I want that trigger in before update event, then what would be the code?
Glyn Anderson 3Glyn Anderson 3
Priyank, the code would stay the same; but I recommend keeping operations that modify an object other than the trigger object in "after" triggers.