You need to sign in to do that
Don't have an account?
SV M
Retrieve contacts related to account
Hi,
I would like to retrieve the contacts related to each account using the apex class. I tried but I am getting the list of accounts on page. Can someone tell me how to achieve this?
//Apex Class
public class operations_class
{
public List<Contact> result{get;set;}
public List<Account> accList = [Select Id from Account];
public operations_class()
{
result=new List<Contact>();
}
public void search()
{
result=[Select FirstName, LastName, Email from Contact Where account.Id=:accList];
}
}
//VF Page
<apex:page controller="operations_class">
<script>
window.onloadstart = function()
{
hello()
{
}
}
</script>
<apex:form>
<apex:actionFunction name="hello" action="{!search}">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="con">
<apex:column value="{!con.FirstName}"/>
<apex:column value="{!con.LastName}"/>
<apex:column value="{!con.Email}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:actionFunction>
</apex:form>
</apex:page>
I would like to retrieve the contacts related to each account using the apex class. I tried but I am getting the list of accounts on page. Can someone tell me how to achieve this?
//Apex Class
public class operations_class
{
public List<Contact> result{get;set;}
public List<Account> accList = [Select Id from Account];
public operations_class()
{
result=new List<Contact>();
}
public void search()
{
result=[Select FirstName, LastName, Email from Contact Where account.Id=:accList];
}
}
//VF Page
<apex:page controller="operations_class">
<script>
window.onloadstart = function()
{
hello()
{
}
}
</script>
<apex:form>
<apex:actionFunction name="hello" action="{!search}">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="con">
<apex:column value="{!con.FirstName}"/>
<apex:column value="{!con.LastName}"/>
<apex:column value="{!con.Email}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:actionFunction>
</apex:form>
</apex:page>
Plaese Try below code.
VFP: Class:
Hope this will help you.
Please Mark it as best answer if you resolve the issue.
Thanks,
Foram Rana
You can also use below code as well.
No need to write controller.
I have gone through your problem.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora