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
Bob Lloyd 2Bob Lloyd 2 

Update parent object field with child object data, both are standard

I'm new to SF and have been successful in some areas and not in others. Hopefully this makes sense and I've used the right terms.

I've been reading about the need to write apex code when you want to update fields between 2 objects when one is standard and one is custom. I have the need to upcate the parent (Account) email (custom field) with the values from the child (contact) when the child is updated. These are both standard objects, so I'm hoping apex isn't required. I haven't had too much success with that (yet).

I've created a lookup between the 2 objects, thereby linking them. There may be many contact records associated with the account record, but only one contact record is set as "head of household". When that record changes the email address (eventually the street address, phone number and other fields will also apply), should transfer to the account's email field.

I attempted to do this using workflow and update field logic, but I was only able to get the update to work when I updated the parent record. I tried to flip it around, puting the flow on the contact record, but I was unable to get the account fields to show as available fields for the update.




geosowgeosow
Hi Bob,

You will need an after insert/update trigger on the Contact object for this.  Here is some very basic code to get you started:

trigger Contact on Contact (after insert, after update) {
    
    Account[] accounts2Update = new Account[]{};
    
    for (Contact c:trigger.new)
    {        
        if (c.AccountId <> null &&
            HeadOfHouseHold__c && 
            (trigger.isInsert || 
             c.Email <> trigger.oldMap.get(c.id).Email || 
             c.HeadOfHouseHold__c <> trigger.oldMap.get(c.id).HeadOfHouseHold__c
            )
           ) 
        {
            accounts2Update.add(new Account(Id=c.AccountId, Email__c=c.Email));
        }
    }
    
    if (!accounts2Update.isempty()) update accounts2Update;

}


Bob Lloyd 2Bob Lloyd 2
Thanks, goesow. I'll have a look through this and work it into our sandbox.