You need to sign in to do that
Don't have an account?
Taresh 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.
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.
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
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. You can put your query results in map directly
https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex6_3.htm
Thanks,
Fahad Akhtar
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.
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.
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...
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
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
Yo will also see a Green Tick in front of the answer which you will select as Best Answer.
Please do it agian.
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