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
PRADEEP YADAV 5PRADEEP YADAV 5 

Update the record using Map

trigger UpdateRecordMap on Update(after Update)
{
  Set<Id>accid = new Set<Id>();
    for(Account a : trigger.new)
    {
          accid.add(a.Id);
    }
    List<Contact>listcon = new List<Contact>();
    Map<Id, Account>mapacc = new Map<Id, Account>
    ([Select Id, BillingCity,
                            (Select Id, MailingState From Contacts) 
                    From Account Where Id In :accid]);
   for(Account a : trigger.new)
    {
             Contact con = new Contact();
             con.MailingState = a.BillingCity;
             listcon.add(con);
    }
    if(listcon.size()>0)
    {
        update listcon;
    }

}
Not Update the Record
David Zhu 🔥David Zhu 🔥
 Please try code below, you may need to fix typo

Map<Id, contact>mapcontact = new Map<Id, contact>
    ([Select Id, account.billingcity,mailing city  from contact where accountid In :accid]);
   for(contacr con: mapcontact.keyset())
    {
             
             con.MailingState = con.account.billlingcuty
             listcon.add(con);
    }
    if(listcon.size()>0)
    {
 ………
PRADEEP YADAV 5PRADEEP YADAV 5
Thanks David Zhu
 
PRADEEP YADAV 5PRADEEP YADAV 5
David Zhu Some Error is Coming 
Invalid loop variable type expected Id was Contact
PRADEEP YADAV 5PRADEEP YADAV 5
Set<Id>accid = new Set<Id>();
    for(Account a : trigger.new)
    {
          accid.add(a.Id);
    }
    List<Contact>listcon = new List<Contact>();
    Map<Id, contact>mapcontact = new Map<Id, contact>
    ([Select Id, account.billingcity,mailingcity  from contact where accountid In :accid]);
    for(contact con: mapcontact.keyset())
    { 
             con.mailingcity = con.account.billingcity;
             listcon.add(con);
    }
    if(listcon.size()>0)
    {
        update listcon;
    }
SarvaniSarvani
Hi Pradeep,

Try using below code I have tested. Please make sure you have enough spaces between variables and datatypes in the code. And I hope your trigger is on Account object. 
trigger UpdateRecordMap on Account(after Update)
{
  Set<Id> accid = new Set<Id>();
  List<Contact> listcon = new List<Contact>();
    for(Account a : trigger.new)
    {
          accid.add(a.Id);
    }
    if(accid.size()>0){
    
    Map<Id, contact> mapcontact = new Map<Id, contact>([Select Id, account.billingcity,mailingcity  from contact where accountid In :accid]);
    for(contact con: mapcontact.values())
    { 
             con.mailingcity = con.account.billingcity;
             listcon.add(con);
    }
    }
    if(listcon.size()>0)
    {
        update listcon;
    }

}

Hope this helps! Please mark as best if it does

Thanks