You need to sign in to do that
Don't have an account?

fetch values from the Map
HI,
I am trying to fetch the values from the map and then updating the field value on Account.
Having trouble, please help.
Thanks
public MapAccount(){
Map<Id, List<Contact>> mapcont = new Map<Id, List<Contact>>();
list<Account> acc= [select id,name from Account limit 5];
set<ID> SetAccount = new set<ID>();
for(Account k: acc){
SetAccount.add(k.Id);
mapcont.put(k.id, new list<contact>());
}
system.debug(mapcont);
for(contact c:[select Accountid, firstname,lastname from contact where accountId IN :SetAccount])
if(mapcont.containskey(c.accountid)){
list<contact> con1 = mapcont.get(c.accountid);
con1.add(c);
mapcont.put(c.id, con1);
}
system.debug(mapcont);
list<aCCOUNT> upacc = new list<Account>();
for(account a1: acc){
if(mapcont.containskey(a1.id)){
account accnew = new account();
//want to fetch the firstname from the mapcont and update account,
//i am unable to get the firstname from the map
accnew.New_contact_Name__c = mapcont.get(a1.ID).firstname;
}
}
}
}
I am trying to fetch the values from the map and then updating the field value on Account.
Having trouble, please help.
Thanks
public MapAccount(){
Map<Id, List<Contact>> mapcont = new Map<Id, List<Contact>>();
list<Account> acc= [select id,name from Account limit 5];
set<ID> SetAccount = new set<ID>();
for(Account k: acc){
SetAccount.add(k.Id);
mapcont.put(k.id, new list<contact>());
}
system.debug(mapcont);
for(contact c:[select Accountid, firstname,lastname from contact where accountId IN :SetAccount])
if(mapcont.containskey(c.accountid)){
list<contact> con1 = mapcont.get(c.accountid);
con1.add(c);
mapcont.put(c.id, con1);
}
system.debug(mapcont);
list<aCCOUNT> upacc = new list<Account>();
for(account a1: acc){
if(mapcont.containskey(a1.id)){
account accnew = new account();
//want to fetch the firstname from the mapcont and update account,
//i am unable to get the firstname from the map
accnew.New_contact_Name__c = mapcont.get(a1.ID).firstname;
}
}
}
}
Your code has a single mistake, your map mapcont has a list of contact on behalf of an accountId that's why
you can't fetch the value from the map. Try the below code it working fine: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
The issue is that you have a Map of Account Id to List of Contacts
When you do something like:
mapcont.get(a1.ID) in this case you will get a list of contacts. A list of contacts won't have a first name, that is an attribute of the actual record.
If you are wanting to get a first name, first you need to identify which contact you want the be pushed back to the account, remembering that an account may have multiple contacts.
you could try something like;
Note, the code sample will only take the first record in the list of contacts, so you may want to order the list somehow.
Regrds
Andrew
Your code has a single mistake, your map mapcont has a list of contact on behalf of an accountId that's why
you can't fetch the value from the map. Try the below code it working fine: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Can you give little bit of explanation on line #19. I coded conName= con.LastName it also worked.
conName=conName+'_'+con.lastname;
I haven't done anything special, I simple concatenate lastName of contacts regarding Account and then update on Account 'New_contact_Name__c' field.
Thanks,
Ajay Dubedi