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

Getting Error "Incompatible element type Id for collection of Account"
Hai All
I have an requirement that when i enter account name in text box click on submit it display all the related contact records
Please check my code
public class conbyacc {
public String accname{set;get;}
public list<Account> acclist{set;get;}
public list<Contact> conlist{set;get;}
public set<Account> accids{set;get;}
public list<Contact> getcons(){
acclist.clear();
conlist.clear();
accids.clear();
acclist=[select Id,name from Account where name=:accname];
for(Integer i=0; i<acclist.size(); i++){
accids.add(acclist[i].Id); // Getting Error
}
conlist=[select name,accountid from Contact where accountid IN:accids];
return conlist;
}
}
Thanks In Advance
Sampath Palli
I have an requirement that when i enter account name in text box click on submit it display all the related contact records
Please check my code
public class conbyacc {
public String accname{set;get;}
public list<Account> acclist{set;get;}
public list<Contact> conlist{set;get;}
public set<Account> accids{set;get;}
public list<Contact> getcons(){
acclist.clear();
conlist.clear();
accids.clear();
acclist=[select Id,name from Account where name=:accname];
for(Integer i=0; i<acclist.size(); i++){
accids.add(acclist[i].Id); // Getting Error
}
conlist=[select name,accountid from Contact where accountid IN:accids];
return conlist;
}
}
Thanks In Advance
Sampath Palli
public set<Account> accids{set;get;}
Better use the following:-
All Answers
public set<Account> accids{set;get;}
Better use the following:-
This set is a collection of Accounts and you are adding just ID's to it,
accids.add(acclist[i].Id);
you will need to add the entire object
accids.add(acclist[i]);
or change the collection type to ID instead of Account.
I write a Vf page and click on button i am getting error please check this image
Thanks In Advance
acclist=[select Id,name from Account where name=:accname];
initialize your list and then assign it to your query.
Also share your VF page code where you are setting the accname.
<apex:page controller="conbyacc">
<apex:form>
<apex:inputText value="{!accname}"/>
<apex:commandButton value="GetRecords" action="{!getcons}" reRender="one"/>
<apex:outputPanel id="one">
<apex:pageBlock>
<apex:pageBlockTable value="{!conlist}" var="c">
<apex:column headerValue="Name">{!c.name}</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
Please update ur controller class with below code. i have tried in my org and everything working great
Please let me know if u still face any issues