0

I am having difficulties with the contact name that I want to be displayed in account list view on my visualforce page.

This is my code:

    <apex:page standardController="Account" recordSetvar="accounts">

 <apex:pageBlock title="Viewing Partners">

  <apex:form id="theForm">
    <apex:panelGrid columns="2">
      <apex:outputLabel value="List View:"/>
      <apex:selectList value="{!filterId}" size="1">
        <apex:actionSupport event="onchange" rerender="list"/>
        <apex:selectOptions value="{!listviewoptions}"/>
      </apex:selectList>
    </apex:panelGrid>
    <apex:pageBlockTable title="Accounts" value="{!Accounts}" var="a">
    <apex:column value="{!a.Name}"/>
    <apex:column value="{!a.Website}"/>
    <apex:column value="{!a.Role__c}"/>
    <apex:column value="{!a.Contacted_by__c}"/>
    <apex:column value="{!a.Status__c}"/>
    <apex:column value="{!a.Contact.Name}"/>
    </apex:pageBlockTable>
<apex:inlineEditSupport/>
<apex:commandButton value="Save" action="{!Quicksave}" />
  </apex:form> 
  </apex:pageBlock>

</apex:page>

The line that i am having problem is :

<apex:column value="{!a.Contact.Name}"/>

Getting this error:

Error: Invalid field Contact for SObject Account

Second Edit: Here is what i want to do upon edit with inlineEditSupport is like is not working properly.

enter image description here

Doesn't Save the new edit when i press go which is a QuickSave button.

Also If there is a way when I press Partners tab which is a vf page I get the MA selected and not the Partner Tab selected. Is there a way to fix this? See image below. enter image description here

I found the solution with the tab active below.

tabStyle="[name of tab without brackets]__tab"
2
  • Are you getting any error or unexpected result ? Commented Apr 13, 2017 at 7:40
  • @MuzammilBajaria yes this is the error that I am having. Error: Invalid field Contact for SObject Account Commented Apr 13, 2017 at 7:45

2 Answers 2

0

"Invalid field Contact for SObject Account"

This is true as account does not have any field "Contact". First, Account is parent so lookup field will be on contact object for account(You can check fields in account and contact object). So "{!a.Contact.Name}" is invalid.

Second, An account can have many contact, so you have to display a list.

As per your requirement if you want to display all the contacts with given account, you have to fetch the contacts using SOQL query and then have to display. Below is the code for displaying accounts and their contacts .

public class StackAccountListController {
List<account> originalAccountList;
List<contact> originalContactList;
List<wrapperClass> listToDisplay{get;set;}

public StackAccountListController(){
    originalAccountList = [select id,name from account];        // Add which ever field you want to display on VF page
    originalContactList = [select id,name,AccountId from contact];
    listToDisplay = new List<wrapperClass>();

}

public List<wrapperClass> getData(){
    for(Account a : originalAccountList){
        wrapperClass wc = new wrapperClass();
        wc.acc = a ;
        List<contact> tempContactList = new List<contact>();
        for(contact c : originalContactList){
            if(c.AccountId == a.Id){
                tempContactList.add(c);
            }
        }
        wc.contactList = tempContactList;
        listToDisplay.add(wc);
    }

    return listToDisplay;

}



public class wrapperClass{
    public account acc{get;set;}
    public List<contact> contactList{get;set;}

    public wrapperClass(){
        acc = new Account();
        contactList = new List<contact>();
    }
}


}

VF page

<apex:page controller="StackAccountListController">

<apex:pageBlock title="Viewing Partners">

    <apex:form id="theForm">
        <apex:pageBlockTable title="Accounts" value="{!Data}" var="a">
            <apex:column value="{!a.acc.Name}"/>
            <apex:column />
            <apex:column >
            <apex:dataList value="{!a.contactList}" var="c">
                <apex:outputText value="{!c.Name}"/>
           </apex:dataList>
            </apex:column>


        </apex:pageBlockTable>
    </apex:form> 
</apex:pageBlock>

I have just demonstrated the logic. You have to use it according to your requirement.

Accept if this answers your question

5
  • Let me know if you still need any help Commented Apr 13, 2017 at 8:01
  • I kinda need help to find a solution. If you have an idea how to fetch the SOQL to get what I need I will highly appreciate it. Commented Apr 13, 2017 at 8:09
  • Ok i will post some code in an hour Commented Apr 13, 2017 at 8:23
  • Thanks @muzammil for your help. Also if you can tell me how to be redirected on that list when i press on any row in the listview to the specific account that will be great. Commented Apr 13, 2017 at 8:24
  • Added code to display contact list. Accept or up vote the answer if this helps Commented Apr 13, 2017 at 10:08
0

If you need to show the contacts on the page without apex controller. You can do something like the below code that will show the contacts on the page.

<apex:page standardController="Account" recordSetvar="accounts">
<apex:pageBlock title="Viewing Partners">
    <apex:form id="theForm">
        <apex:panelGrid columns="2">
            <apex:outputLabel value="List View:"/>
            <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" rerender="list"/>
                <apex:selectOptions value="{!listviewoptions}"/>
            </apex:selectList>
        </apex:panelGrid>
        <apex:pageBlockTable title="Accounts" value="{!accounts}" var="a">
            <apex:column value="{!a.Name}"/>
            <apex:column value="{!a.Website}"/>
            <apex:column headerValue="Contact List">
                <apex:pageBlockTable value="{!a.Contacts}" var="c">
                    <apex:column value="{!c.Name}"/>
                </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
        <apex:inlineEditSupport />
        <apex:commandButton value="Save" action="{!Quicksave}" />
    </apex:form> 
</apex:pageBlock>

5
  • This looks really good @Dan there is only one problem. The button doesn't save the edits and also if there is a way to add a functionality like the real list when i click to get redirected directly to the account.Also i see that the name header is shown double. Is there a way to show it once? Thanks Darko Commented Apr 14, 2017 at 15:38
  • What is the issue with the name header? I don't understand the issue you are running into. - Dan Commented Apr 17, 2017 at 19:45
  • Hey Dan, So when I edit a field with the <apex:inlineEditSupport /> command added I am not able to save the edits. It gets me to another page but without save. Commented Apr 18, 2017 at 8:09
  • I've edited the question, please check it out. I hope it looks more clear now. Commented Apr 18, 2017 at 8:16
  • So I have been looking around and I can't seem to find a good solution to your problem where you can't save your changes. I think the best solution is to create a custom controller that will handle the save method. I also found an post that shows how to get the same functionality of using the list views as your currently have but with a custom controller. Let me know if this helps. jitendrazaa.com/blog/salesforce/… Commented Apr 18, 2017 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .