+ Start a Discussion
Heather DeMalioHeather DeMalio 

Apex Trigger to update a contact field from an account field


I need an Apex Trigger to update a date field on a Contact Record with the date field from the related Account Record. Will also need Apex Class. The Contact Record is not always edited so a workflow or process doesn't cut it.

Thank you
What is the date of contact you wanna update and what is the date of account you want to get?
Swayam  AroraSwayam Arora
Why don't you use formula field as the Date field in Contacts referring to the Date field in Account.
Heather DeMalioHeather DeMalio
Bogdan, it is not a specific date. It is a date field that on the Account record that is entered when something happens. I need this to populate the date field on the Contact Record without the Contact record having to be edited.

Swayam, it would be beautiful to be able to use a formula field however, I can't run a time dependent workflow off the Contact date field as a formula.
Ok, so you want to change LastModifiedDate on Contact row, if Accout LastModifiedDate was changed. We can update Contact without changing data emediately after changing the Account, but we can't get the same LastModifiedDate on both of them all the time, depends on your activity and server speed, difference might be few seconds sometimes i guess.
The trigger will be something like that:
trigger ChangeContactLastModifiedDate on Account (after update) {
    List<Contact> contactList = new List<Contact>();
    for(Account acc: [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
        if(acc.Contacts.size() > 0){
    update contactList;