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
Timmy AhluwaliaTimmy Ahluwalia 

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;

            }
        }
         }
    }
Best Answer chosen by Timmy Ahluwalia
Ajay K DubediAjay K Dubedi
Hi Timmy,

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:
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>());
        }
        for(contact c:[select Accountid,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);
            }
        for(account a1: acc){
            if(mapcont.containskey(a1.id)){
                String conName='';
                for(Contact con:mapcont.get(a1.ID)){
                    conName=conName+'_'+con.lastname;
                }
                if(conName!='' || conName!=null){
                a1.New_contact_Name__c=conName;
                }
            }
            }system.debug('accList->'+acc);
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

Andrew GAndrew G
Hi 
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;
account.New_contact_Name__c = mapcont.get(a1.Id)[0].firstName;

OR - if the code doesn't like sitting together

List<Contact> contacts = mapcont.get(a1.Id);
account.New_contact_Name__c = contacts[0].firstName;

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

 
Ajay K DubediAjay K Dubedi
Hi Timmy,

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:
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>());
        }
        for(contact c:[select Accountid,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);
            }
        for(account a1: acc){
            if(mapcont.containskey(a1.id)){
                String conName='';
                for(Contact con:mapcont.get(a1.ID)){
                    conName=conName+'_'+con.lastname;
                }
                if(conName!='' || conName!=null){
                a1.New_contact_Name__c=conName;
                }
            }
            }system.debug('accList->'+acc);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Timmy AhluwaliaTimmy Ahluwalia
Thanks Ajay your solution worked.
Can you give little bit of explanation on line #19. I coded conName= con.LastName it also worked.
conName=conName+'_'+con.lastname;
Ajay K DubediAjay K Dubedi
Hi Timmy,
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