You need to sign in to do that
Don't have an account?
Srikanth Dorepalli
My task is to write a soql to fetch all the account records with corresponding contacts and create a map with account name as key and list of corresponding contacts as values
APEX CLASS:
public class fetch1 {
public list<account> acc{set;get;}
public map<string,list<contact>> mp{set;get;}
public fetch1(){
acc=[select name,(select lastname,firstname from contacts) from account ];
list<contact> con=new list<contact>();
mp=new map<string,list<contact>>();
for(account a:acc){
for(contact c:a.contacts){
con.add(c);
}
mp.put(a.name,con);
}
}
}
VF:
<apex:page controller="fetch1" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column headerValue="ACCOUNTS" value="{!a.name}"/>
<apex:column headerValue="RELATED CONTACTS" >
<apex:pageBlockTable value="{!mp}" var="cont">
<apex:column value="{!mp[cont].firstname}"/>
<apex:column value="{!mp[cont].lastname}"/>
</apex:pageBlockTable>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
ERROR:
There are no errors in code.But when i am going for a preview of vf page i am getting this error "Incorrect parameter type for subscript. Expected Number, received Text "
public class fetch1 {
public list<account> acc{set;get;}
public map<string,list<contact>> mp{set;get;}
public fetch1(){
acc=[select name,(select lastname,firstname from contacts) from account ];
list<contact> con=new list<contact>();
mp=new map<string,list<contact>>();
for(account a:acc){
for(contact c:a.contacts){
con.add(c);
}
mp.put(a.name,con);
}
}
}
VF:
<apex:page controller="fetch1" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column headerValue="ACCOUNTS" value="{!a.name}"/>
<apex:column headerValue="RELATED CONTACTS" >
<apex:pageBlockTable value="{!mp}" var="cont">
<apex:column value="{!mp[cont].firstname}"/>
<apex:column value="{!mp[cont].lastname}"/>
</apex:pageBlockTable>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
ERROR:
There are no errors in code.But when i am going for a preview of vf page i am getting this error "Incorrect parameter type for subscript. Expected Number, received Text "
I found two issues:
1) The list of contact should be re-initialize, otherwise it will keep adding previous contacts
2) Declaring "{!mp[cont].firstname}" in <apex:column value="{!mp[cont].firstname}"/>, is incorrect because "mp[cont]" actually returns list of contacts. Therefore List<Contact>.firstname throws exception.
Here we go complete correct code :
Apex Controller:
VF page:
Thanks
Gulshan Raj
All Answers
I found two issues:
1) The list of contact should be re-initialize, otherwise it will keep adding previous contacts
2) Declaring "{!mp[cont].firstname}" in <apex:column value="{!mp[cont].firstname}"/>, is incorrect because "mp[cont]" actually returns list of contacts. Therefore List<Contact>.firstname throws exception.
Here we go complete correct code :
Apex Controller:
VF page:
Thanks
Gulshan Raj
Here is the more optimized controller:-
If you are not using "acc" on your VF page then you can eliminate this as well like this:-
Hope it helps :)