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 address using map

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​
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