+ Start a Discussion
Vijay ummedaVijay ummeda 

trigger to update the home address on conatct object if it's updated on account object

Hi  Team,

I need to write a trigger to update the address on the contact object if a user select a checkbox on account filed address then   copy  the same values into conatct object 


trigger accountcontact on account(after insert,after update)
{
List<contact> contactupdate list = new List<contact>();
{
for (account a : trigger.new){
contactupdate.add(a);
}
}
List<account> Updatedcontact = new List<account>([select id,name,accountid,(select id,name from contact where accountid in :accountid]);
for(account acc : trigger.new){
if(acc.address =='true')
contact.address = acc.address;
contact.city = accc.city;
contact.pincode= acc.pincode;
contact.district = acc.district;
updatedcontact.add(acc);
}
insert updatedcontact;
}
sachinarorasfsachinarorasf
Hi  a . vijay ,
Here is the code which suits your requirement.
This trigger is running on the after insert and after update.
trigger AccountTriger on Account (after insert, After Update) {
 if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)){
        CopyAddressIntoContact.copyaddress(trigger.newMap);
    }
}
below is the apex class which is a call from the trigger. When You insert a new account then it will create a new account with the same address otherwise it will update the existing contact address.
public class CopyAddressIntoContact {
    public static void copyaddress(Map<Id,Account> accountMap){
        try{
            List<Contact> newContactList = new List<Contact>();
            List<Contact> updateContactList = new List<Contact>();
            Set<Id> accountId=new Set<Id>();
            for(Id key:accountMap.keySet())
            {
                if(accountMap.get(key).Address__c){
                    accountId.add(Key);
                    Contact c = new Contact();
                    c.AccountId = key;
                    c.LastName = accountMap.get(key).Name;
                    c.MailingStreet     = accountMap.get(key).BillingStreet;
                    c.MailingCity       = accountMap.get(key).BillingCity;
                    c.MailingState      = accountMap.get(key).BillingState;
                    c.MailingPostalCode = accountMap.get(key).BillingPostalCode;
                    c.MailingCountry    = accountMap.get(key).BillingCountry;
                    newContactList.add(c);
                }
            }
            if(accountId.size() > 0){
                updateContactList = [select id, 
                               MailingStreet, 
                               MailingCity, 
                               MailingState, 
                               MailingPostalCode, 
                               MailingCountry, 
                               AccountId 
                               from Contact Where AccountId IN: accountId Limit 50000];
            }
            if(updateContactList.size() > 0){
                for (Contact c : updateContactList){
                    c.MailingStreet     = accountMap.get(c.AccountId).BillingStreet;
                    c.MailingCity       = accountMap.get(c.AccountId).BillingCity;
                    c.MailingState      = accountMap.get(c.AccountId).BillingState;
                    c.MailingPostalCode = accountMap.get(c.AccountId).BillingPostalCode;
                    c.MailingCountry    = accountMap.get(c.AccountId).BillingCountry;
                }            
                Update updateContactList;
            }else{
                insert newContactList;
            }
            System.debug('updated Contact :: '+updateContactList);
            System.debug('inserted Contact :: '+newContactList);
        }Catch(exception e){
            System.debug('Error '+e.getMessage()+' line number '+e.getLineNumber());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
Malika Pathak 9Malika Pathak 9

Trigger:-
trigger ContactAddresUpdate on Account (After insert,After Update)
{
ContactAddresUpdate_Handler.Address(Trigger.new);
}

Handler:-
public class ContactAddresUpdate_Handler
{
public static void Address(List<Account> AccList)
{
Map<Id,Account> mapOfIdToAccount = new Map<Id,Account>();
List<contact> lstOfUpdcontacts = new List<Contact>();
for(Account acc:AccList){
mapOfIdToAccount.put(acc.id,acc);
}

List<Contact> contactList =[select id,AccountId,MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry
from Contact where AccountId IN:AccList];

for(Contact con: contactList){
if(mapOfIdToAccount.get(con.AccountId).Contact_Address__c == True)
{
//con.AccountId = mapOfIdToAccount.get(con.AccountId).Id;
con.MailingStreet = mapOfIdToAccount.get(con.AccountId).BillingStreet;
con.MailingCity = mapOfIdToAccount.get(con.AccountId).BillingCity;
con.MailingState = mapOfIdToAccount.get(con.AccountId).BillingState;
con.MailingPostalCode = mapOfIdToAccount.get(con.AccountId).BillingPostalCode;
con.MailingCountry = mapOfIdToAccount.get(con.AccountId).BillingCountry;
lstOfUpdcontacts.add(con);

}
}

if(!lstOfUpdcontacts.IsEmpty())
{
update lstOfUpdcontacts;
System.debug('lstOfUpdcontactssssssss>>>'+lstOfUpdcontacts);
}
}
}

If you find this solution is helpful for you please mark best answer