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
123_P123_P 

please solve this problem on trigger if i update the contact address it sholud change the associated account address using map

Best Answer chosen by 123_P
Ajay K DubediAjay K Dubedi
Hi Aarush,

Hope this will help you

trigger UpdateContactAdd on Contact(after Update) {
  
    if(trigger.isAfter)
    {    
        ChangeAccAddress.usingContactAddress(trigger.new);    
    }
}


public class ChangeAccAddress {
    public static void usingContactAddress(List<Contact> conList)
    { 
         map<Id,Contact> conidVsConMap = new map<Id,Contact>
         (
             [Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry,  MailingPostalCode from Contact
             WHERE Id IN:conList]
          );
        List<Account> accList = new List<Account>();
        for(Contact con : conList) 
        {
            Account accObj = new Account();
            accObj.Id = conidVsConMap.get(con.Id).AccountId;
            accObj.BillingStreet = conidVsConMap.get(con.Id).MailingStreet;
            accObj.BillingState = conidVsConMap.get(con.Id).MailingState;
            accObj.BillingPostalCode = conidVsConMap.get(con.Id).MailingPostalCode;
            accObj.BillingCountry = conidVsConMap.get(con.Id).MailingCountry;
            accObj.BillingCity = conidVsConMap.get(con.Id).MailingCity;
            accList.add(accObj);
        }
        system.debug('account Address>>>>>>>>>' + accList);
        if(accList.size() > 0)
            update accList ;
    }
}


Thank you
Ajay Dubedi

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code for the same try to change according to your requirements.
 
trigger CaseAfterInsertUpdate on Account(after insert, after update) {

    List<Contact> accList = new List<Contact>();
    Map<Id,List<Contact>> AccountMap = new Map<Id,List<Contact>>();

    if(Trigger.isUpdate){

        for(Contact con : [SELECT Id,AccountId FROM C0ntact WHERE AccountId IN :Trigger.newMap.keySet()])

            if(!AccountMap.containskey(con.AccountId){
                AccountMap.put(con.AccountId,new List<Contact>());
            }
            AccountMap.get(con.AccountId).add(con); 
        }
    }

    for(Account c:Trigger.new) {

        system.debug('hfeffht'+c.ID);

        if(!AccountMap.containskey(c.Id)) { //code for insert only

            Contact con = new Contact(LastName=c.Name,
                                MailingStreet=c.BillingStreet,
                                MailingCity=c.BillingCity,
                                MailingPostalCode=c.BillingPostalCode,
                                MailingCountry=c.BillingCountry,
                                AccountId=c.ID);
            accList.add(con);
            system.debug('AccountId'+con.AccountId);

         }else { //code for update account

            for(Contact con : AccountMap.get(c.Id)){ //get all contacts under account and update.

                con.MailingCity = c.BillingCity;
                con.MailingPostalCode = c.BillingPostalCode;
                con.MailingCountry = c.BillingCountry;
                accList.add(con);
            }
         }
    }


    try {
        upsert accList;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

Best Regards,
Sandhya​
Abhishek BansalAbhishek Bansal
Hi Aarush,

Please try with the below code:
trigger updateAccount on Contact(after update) {
	Set<Id> accIds = new Set<Id>();
	for(Contact con : trigger.new) {
		if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress) {
			accIds.add(con.AccountId);
		}
	}
	
	if(accIds.size() > 0) {
		Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select id, BillingAddress from Account where Id IN :accIds]);
		List<Account> updateAccountList = new List<Account>();
		for(Contact con : trigger.new) {
			if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress && mapOfAccounts.containsKey(con.AccountId)) {
				mapOfAccounts.get(con.AccountId).BillingAddress = con.MailingAddress;
				updateAccountList.add(mapOfAccounts.get(con.AccountId));
			}
		}
		
		if(updateAccountList.size() > 0){
			update updateAccountList;
		}
	}
}
//Please use the appropriate fields in place of MailingAddress(Contact) and BillingAddress(Acccount) as per your requirement and data strucgure and take care of the syntax.

Let me know in case there is an issue.

Thanks,

Abhishek Basal.

Ajay K DubediAjay K Dubedi
Hi Aarush,

Hope this will help you

trigger UpdateContactAdd on Contact(after Update) {
  
    if(trigger.isAfter)
    {    
        ChangeAccAddress.usingContactAddress(trigger.new);    
    }
}


public class ChangeAccAddress {
    public static void usingContactAddress(List<Contact> conList)
    { 
         map<Id,Contact> conidVsConMap = new map<Id,Contact>
         (
             [Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry,  MailingPostalCode from Contact
             WHERE Id IN:conList]
          );
        List<Account> accList = new List<Account>();
        for(Contact con : conList) 
        {
            Account accObj = new Account();
            accObj.Id = conidVsConMap.get(con.Id).AccountId;
            accObj.BillingStreet = conidVsConMap.get(con.Id).MailingStreet;
            accObj.BillingState = conidVsConMap.get(con.Id).MailingState;
            accObj.BillingPostalCode = conidVsConMap.get(con.Id).MailingPostalCode;
            accObj.BillingCountry = conidVsConMap.get(con.Id).MailingCountry;
            accObj.BillingCity = conidVsConMap.get(con.Id).MailingCity;
            accList.add(accObj);
        }
        system.debug('account Address>>>>>>>>>' + accList);
        if(accList.size() > 0)
            update accList ;
    }
}


Thank you
Ajay Dubedi
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Aarush,

Just added the code for insert
trigger updateAccount on Contact(after insert, after update) {
	Set<Id> accIds = new Set<Id>();
	for(Contact con : trigger.new) {
		if(trigger.isInsert && con.AccountId != null) {
			accIds.add(con.AccountId);
		}
		else if(trigger.isUpdate && con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress) {
			accIds.add(con.AccountId);
		}
	}
	if(accIds.size() > 0) {
		Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select id, BillingAddress from Account where Id IN :accIds]);
		List<Account> updateAccountList = new List<Account>();
		for(Contact con : trigger.new) {
			if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress && mapOfAccounts.containsKey(con.AccountId)) {
				mapOfAccounts.get(con.AccountId).BillingAddress = con.MailingAddress;
				updateAccountList.add(mapOfAccounts.get(con.AccountId));
			}
		}
		
		if(updateAccountList.size() > 0){
			update updateAccountList;
		}
	}
}
This trigger will update the BillingAddress on Account if MailingAddress is changed on Contact and this is what you mentioned in your requirement. I am not sure what do you mean by your last reply. If your requirement is something else then you can modify the code as per your requirement. Please take this code as a guidance and not as a complete solution. If that solves your query then please close this question so that it can help others.

Thanks,
Abhishek Bansal.
123_P123_P
thnx ajay
 
123_P123_P
Ajay sir can you please tell how i can find recent contact when contact comes in bulk and then recent contact address change to account adress means i want this code for bulk only recent contact address change the account adress when contact insert in bulk