You need to sign in to do that
Don't have an account?
Chitral 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;
}
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;
}
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
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.
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. :) )
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.