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
Taresh PandeyTaresh Pandey 

How to display 'Contact Name' & 'Contact ID' (fields of Contact) from Account Object by using MAP. ??

Hello all,

First of all I say thank you guys for helping me on my earlier problem. For this task some limitations are there:-
1- Use only one query where ever you want.
2- You can not use sub-query for the problem.

Thank you all in advance.
Best Answer chosen by Taresh Pandey
Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find your required code :

List<Contact> contactList = new List<Contact>([Select Name,Id,AccountId from Contact]);

Map<Id,List<Contact>> accConMap = new Map<Id,List<Contact>>();
List<Contact> conList;
for(Contact con : contactList){
    if(accConMap.containsKey(con.AccountId)){
        accConMap.get(con.AccountId).add(con);
    }
    else{
        conList = new List<Contact>();
        conList.add(con);
        accConMap.put(con.AccountId,conList);
    }
}


Now you have your map from which you can get the list of contact associated with a single account.

Let me know if you need more help.

Regards,
Abhishek.

All Answers

Fahad-AkhtarFahad-Akhtar
Hi Taresh,
Please explain your issue in more detail, If you want to query multiple objects you have two options one is to do subquery which you said you dont want to use, the other option is to query child and access parent fields.
SELECT id,Name,Account.Name  FROM Contact
You can put your query results in map directly 
 
Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex6_3.htm

Thanks,
Fahad Akhtar
Abhishek BansalAbhishek Bansal
Hi Taresh,

Please find your required code :

List<Contact> contactList = new List<Contact>([Select Name,Id,AccountId from Contact]);

Map<Id,List<Contact>> accConMap = new Map<Id,List<Contact>>();
List<Contact> conList;
for(Contact con : contactList){
    if(accConMap.containsKey(con.AccountId)){
        accConMap.get(con.AccountId).add(con);
    }
    else{
        conList = new List<Contact>();
        conList.add(con);
        accConMap.put(con.AccountId,conList);
    }
}


Now you have your map from which you can get the list of contact associated with a single account.

Let me know if you need more help.

Regards,
Abhishek.
This was selected as the best answer
Taresh PandeyTaresh Pandey
Hey Fahad !
Thanks for reply ... but as I mentioned  limitation in that question that I can't use sub-query while the problem. The link you have given me is really helpful. 
Taresh PandeyTaresh Pandey
Hey Abhishek,
I think I have solved the problem. Please check it out.

public class Accountcalling
{
    public map<string,List<Contact>> getcontact()
    {
        List<Contact> contactlogs = [SELECT AccountId,Name,ID FROM Contact];
        map<string,List<Contact>> getcontact = new map<string,List<Contact>> ();
        
            for(Contact cont: contactlogs)
            {
               if (getcontact.containskey(cont.accountid))
               {
                   List<Contact> blanklist = getcontact.get(cont.accountid);
                   contact updatecontact = new contact();
                       updatecontact.ID = cont.ID;
                       updatecontact.Description = cont.Name;
                   blanklist.add(updatecontact);
                 getcontact.remove(cont.accountid);
                 getcontact.put(cont.accountid,blanklist);
               }
               else
               {
                   List<contact> field1 = new List<contact> ();
                   contact Name1 = new contact();
                       Name1.Description = cont.Name;
                       Name1.ID = cont.ID;
                   field1.add(Name1);
                   getcontact.put(cont.accountid, field1);
               }
            }  return getcontact;
    }
}



If you have any other solution; please let me know... 
Abhishek BansalAbhishek Bansal
Taresh,

I could not understand what are you trying to achieve with this code.
I have already provided you the code in one of the post mentioned above.
Please have a look and let me know if that code meet your requirement or not ?
If no, than please explain your requirements clearly so that i can help you.

Regards,
Abhishek
Taresh PandeyTaresh Pandey
Abhishek I was try to make a contact_book. and my code debugged successfully. The code you have provided me; really healped me so thank you.  
Abhishek BansalAbhishek Bansal
Fine Taresh, Please mark is as SOLVED if you do not have any further queries on it.
Taresh PandeyTaresh Pandey
Abhishek not able to find the “Accept as Solution” button on the post. where is this button ?
Abhishek BansalAbhishek Bansal
Hi Taresh,

In order to mark your question as SOLVED you have to select a best answer than automatically your question will be marked as Solved.
You will see a Best Answer link in every reply.
Select the Reply which you think is best for you than your question will be marked as SOLVED.

Regards,
Abhishek
Taresh PandeyTaresh Pandey
I have already selected answer of yours' as Best Answer. .  Is my question showing the lebel of "Solved" ? 
Abhishek BansalAbhishek Bansal
No Taresh, you have not selected any answer as Best Answer so your question is alos not showing as Solved
Yo will also see a Green Tick in front of the answer which you will select as Best Answer.
Please do it agian.
Abhishek BansalAbhishek Bansal
Hi Taresh,

Just clearing out the confusion, I was talking about this question and you were telling about your previous question.
Yes, Your previous question is marked as SOLVED.
Please mark this question also as solved if you do not have any further queries.
You can also select your own answer as BEST Answer.

Abhishek