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
User One 35User One 35 

I want to auto update Account Address in Contact, when Contact is created using process builder and apex

I want to auto update Account address when I create Contact and save account address should save in contact address by using process builder and Apex please can anyone help me on this 
Sandeep YadavSandeep Yadav
Create a process builder on contact object and relate your contact address field with account address field
Or 
You can this with the help of trigger. 
I hope this helps. 
User One 35User One 35
can you please help me how to write trigger for this 
User One 35User One 35
I did with process builder but I want to do with process builder and apex 
 
v varaprasadv varaprasad
Hi ,

Please check once following sample code. 

 
trigger sharem on Contact(before insert, before update) {

    set<id> aid = new set<id>();

    for (Contact cont: trigger.new){
	   if(cont.accountid != null)
        aid.add(cont.accountid);       
    }
	
	map<id,Account> accMap = new Map<id,account>([select id, name, address from Account where id in: aid]);
	
	for (Contact cont: trigger.new){
	       cont.address = accMap.get(cont.accountid).address
	}
	
  
}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
User One 35User One 35
Hi Varaprasad,

Thanks for replay, by using Apex and Process Builder to Auto update the Account address in the Contact Address when I save the Contacts 
Dev_AryaDev_Arya
hi User One,

what you are looking for is invocable method. To call apex from process builder you need to annotate your apex method with @InvocableMethod annotation. Here it goes.
 
// Apex class
public class ApexLearning {
    public class Request {
        @InvocableVariable
        public Id accountId;
        @InvocableVariable
        public Id contactId;
    }

    @InvocableMethod(label='Get Account Address' description='update contact address with acconnt address')
    public static void updateContact(List<Request> ids) {
        System.debug('*****Process builder output- accountId:' + Ids.get(0).accountId);
        System.debug('*****Process builder output- contactId:' + Ids.get(0).contactId);
        Account acc = [select ShippingCity, ShippingCountry, ShippingState, ShippingStreet, ShippingPostalCode from Account where id = :ids.get(0).accountId];
        Contact c = [select MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode from Contact where id = :ids.get(0).contactId];
        c.MailingCity = acc.ShippingCity;
        c.MailingStreet = acc.ShippingStreet;
        c.MailingState = acc.ShippingState;
        c.MailingCountry = acc.ShippingCountry;
        c.MailingPostalCode = acc.ShippingPostalCode;
        update c;
    }
}

I will clarify the use of @invocablevariable and why parameters are sent in this patten, in a seperate and share it here.

In your process builder, you can call action as Apex, wizard will guide you thru it:

User-added image

This is tested and verfiied. Hope this helps. Watch out for the post with more clarifications.

Cheers, Dev  
Sandeep YadavSandeep Yadav
Hello 135
Try this trigger --
This trigger update account billing address when you create a contact.
trigger UpdateAccountAddressField on Contact (after insert, after update) 
{
    set<Id> ids = new set<Id>();
    
    for(contact c : Trigger.new)
    {
        if(c.accountId != null)
        {
            ids.add(c.accountId);
        }
    }
    
    List<Account> updateList = new List<Account>();
    List<Account> acList = [select id,billingcountry,billingcity,billingstreet from account where id IN: ids];
    
    for(Account ac : acList)
    {
        for(Contact c : Trigger.new)
        {
            ac.billingcountry = c.mailingcountry;
            ac.billingcity = c.mailingcity;
            ac.billingstreet = c.mailingstreet;
            
            updateList.add(ac);
        }
    }
    
    update updateList;
}
Hope this clear you !!