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
Chitral ChaddaChitral Chadda 

trigr account

populate list of all address_for_contac__c field on contac to Final_address_of_contacts__c on account field
suppose (addres fields)
contact 1 : a
contact 2 :c
so in account
Final address of contacts :a ,c

in this trigger its coming
Final address of contacts : http://,a


trigger listOfContactsOnAccount on contact (after insert , after update)

{ set<id> con = new set<id>();
 
           if( trigger.isInsert|| trigger.isUpdate)
           for(contact c : trigger.new)
            {
            con.add(c.AccountId);
            }
  
            if(trigger.isDelete)
            for(contact c: trigger.old)
            {
             con.add(c.AccountId);
            }
          
    //2.create map
  
    List<contact> cont = [ Select AccountId , Address_for_contact__c from contact where AccountId IN : con ]; 
    Map<id,contact> maps = new map<id,contact>();
  
  
            for(contact c :cont)
            {
          
            maps.put(c.accountId,c);
          
            }
  
    //3 create trigger.new to match each recore
      list<account> acConAddress = new list<account>();
      String l  = '';
  

  
     list<Account>  ac = [select id, Final_Address_of_Contacts__c from account where Id IN :con];
     
  
           for(account act : ac)
         
             {    contact co = maps.get(act.Id);
                  string local= co.Address_for_Contact__c;
                  l=l+','+local;        
                  act.Final_address_of_contacts__c = l;
                  acConAddress.add(act);
             }
         update acConAddress;
}
Best Answer chosen by Chitral Chadda
Shyam BhundiaShyam Bhundia
try the code below.  I've update some of your variable names to make it a bit clear.

trigger listOfContactsOnAccount on contact (after insert , after update){ 
  set<id> accountIdSet = new set<id>();
  
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }

  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }
            
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ]; 
    
    //using a map of lists instead of a map of contacts
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
  
    List<Account> accountsToUpdate= new List<Account>();
    //3 go thru each account in the accountContactsMap
    for(ID accID : accountContactsMap.keySet()){
      String finalAddressofContacts = '';
      //get the list of contacts
      for(Contact aContact : accountContactsMap.get(accID)){
        finalAddressofContacts += aContact.Address_for_Contact__c;
      }
      //as we have the acc id and we are only updating the Final_address_of_contacts__c, we can create a new instance
      //of the account record, add it to a list and UPDATE that list.
      accountsToUpdate.add(new Account(id = accID, Final_address_of_contacts__c = finalAddressofContacts));
    }
   
   //update the accounts.
   update accountsToUpdate;
}



As you can see, we are using a maps of lists.  The key is the account id and the value is a list of related contacts.  

One thing to note is that you dont need the extra query for the account.  As you are not using the account to read any values, it doesn't need to be queried for, you can create an instance of that record using the account id and update it.

(p.s. I've not had a chance to test the code, but it does compile. :) )

All Answers

Shyam BhundiaShyam Bhundia
In your model can an account have more then one contact?
Reason for asking is because when you are creating the account ID and contact map, if you have more then one contact your map will always have one entry for the account with the last contact.

If the account has more then one contact then it will be a good idea to use a map of lists (map<id, List<Contact>) and then go over that list and add the addresses to the final address of contacts field.

Hope this makes sense.
Chitral ChaddaChitral Chadda
@shyam Yes it can hv more thn 1 contact in one account Coupd u help a lio more on this Map> Hw to proceed further Cz i hvnt used list inside a ma what chnges i make in d code
Shyam BhundiaShyam Bhundia
try the code below.  I've update some of your variable names to make it a bit clear.

trigger listOfContactsOnAccount on contact (after insert , after update){ 
  set<id> accountIdSet = new set<id>();
  
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }

  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }
            
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ]; 
    
    //using a map of lists instead of a map of contacts
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
  
    List<Account> accountsToUpdate= new List<Account>();
    //3 go thru each account in the accountContactsMap
    for(ID accID : accountContactsMap.keySet()){
      String finalAddressofContacts = '';
      //get the list of contacts
      for(Contact aContact : accountContactsMap.get(accID)){
        finalAddressofContacts += aContact.Address_for_Contact__c;
      }
      //as we have the acc id and we are only updating the Final_address_of_contacts__c, we can create a new instance
      //of the account record, add it to a list and UPDATE that list.
      accountsToUpdate.add(new Account(id = accID, Final_address_of_contacts__c = finalAddressofContacts));
    }
   
   //update the accounts.
   update accountsToUpdate;
}



As you can see, we are using a maps of lists.  The key is the account id and the value is a list of related contacts.  

One thing to note is that you dont need the extra query for the account.  As you are not using the account to read any values, it doesn't need to be queried for, you can create an instance of that record using the account id and update it.

(p.s. I've not had a chance to test the code, but it does compile. :) )
This was selected as the best answer
Chitral ChaddaChitral Chadda
it works perfect. one last thing for(ID accID : accountContactsMap.keySet()) can u pls xplain this line and keyset also
Shyam BhundiaShyam Bhundia
awesome!

So you know that Maps are bascially key value pairs...accountContactsMap.keySet() will get the list of keys.  In this case it will return a list of account ids.  We are using the account id list to go thru the map to get the values.

e.g. 
map = (1=>a, 2=>b, 3=>c)
doing map.keySet(), you will get back a list containing (1,2,3).  Then you can use the keys to access the values in the map.

Hope that makes sense.
Chitral ChaddaChitral Chadda
Perfect xplaintion !! kudos bro thnkyou