[SalesForce] Issue displaying lookup inputField through wrapper class results

Im having trouble getting the Financial_Account__c lookup field to display properly on my visualforce page. I am loading a wrapper class that contains Financial_Account_c and Holding_c. The Holding__c object has a lookup on it to Financial_Account_c. Im unable to get the field to display like it should as an inputField through Holding_c.Financial_Account__c nor directly to Financial_Account__c. All suggestions are appreciated.

Controller

public class ctrl {

    public list<holdingsWrapper> holdingList {get;set;}
    public list<Holding__c> holdingResults {get;set;}

    public ctrl(){ 
        this.init();
    }

    private void init(){

        holdingList = new list<holdingsWrapper>();

        String mhQuery = 'SELECT Name, '; 
        mhQuery += 'Financial_Account__c, ';
        mhQuery += 'Advisor_ID__c ';
        mhQuery += 'FROM Holding__c ';

        holdingResults = Database.query(mhQuery);

        for(Holding__c m : holdingResults) 
        {
            holdingList.add(new holdingsWrapper(m.Financial_Account__r, m));
        }

    }

    public class holdingsWrapper
    {
        public Financial_Account__c fa {get; set;}
        public Holding__c maho {get; set;}

        public holdingsWrapper(Financial_Account__c a, Holding__c mh)
        {
            fa = a;
            maho = mh;
        }
    }

} 

Visualforce Page

<apex:page controller="ctrl" title="Financial Accounts" sidebar="false">

<apex:form id="myMHForm">
    <apex:outputPanel id="formPanel" layout="inline">

        <apex:pageBlock title="Financial Accounts" id="muselectedlist"> 

            <apex:pageBlockTable value="{!holdingList}" var="mhItem" id="pgTable1">

                <apex:column headerValue="Financial Account" id="movetoaccount">
                    <apex:inputField value="{!mhItem.fa}" />
                    <!-- <apex:inputField value="{!mhItem.maho.Financial_Account__c}" /> -->
                </apex:column>
            </apex:pageBlockTable>

        </apex:pageBlock>

    </apex:outputPanel>
    </apex:form>

</apex:page>

ISSUE 1: The following does not allow me to save my VF page:

<apex:inputField value="{!mhItem.fa}" />

ERROR:
Save error: Could not resolve the entity from value binding '{!mhItem.fa}'. can only be used with SObjects, or objects that are Visualforce field component resolvable. line 0 Force.com save problem

ISSUE 2: The following version saves, but only displays text and NOT the input field with the magnify glass:

<apex:inputField value="{!mhItem.maho.Financial_Account__c}" />

How do I get the field to display properly?

All suggestions are appreciated!

Best Answer

ISSUE 1 :
You are trying to bind an instance of Financial_Account__c to the inputField as per your wrapper class (your fa is type of Financial_Account__c). This doesn't make sense with an inputField. See the value attribute here in the documentation. You have to bind a field of an object as the value

ISSUE 2 :
You can directly bind the holdingResults list since the Financial_Account__c is a lookup.

<apex:pageBlockTable value="{!holdingResults}" var="mhItem" id="pgTable1">
    <apex:column headerValue="Financial Account" id="movetoaccount">
        <apex:inputField value="{!mhItem.Financial_Account__c}" />                    
    </apex:column>
</apex:pageBlockTable>


UPDATE : with the above code, you should be able to see a table like as below. pageblocktable
Retrieve all necessary fields from Holding__c obj according to the relationships in your query (Name,Financial_Account__c,Financial_Account__r.Name,etc.) and just set the Holding__c in your constructor (I don't see a value of setting the Financial_Account__c as well here). Then you can bind them in your VF page as below.

<apex:pageBlockTable value="{!holdingList}" var="mhItem" id="pgTable1">
    <apex:column headerValue="Financial Account" id="movetoaccount">
        <apex:inputField value="{!mhItem.maho.Financial_Account__c}" />                    
    </apex:column>
    <apex:column headerValue="Test" id="movetoaccount1">
        <apex:inputField value="{!mhItem.maho.Financial_Account__r.Name}" />                    
    </apex:column>
    <apex:column headerValue="Halding Name" id="movetoaccount2">
        <apex:inputField value="{!mhItem.maho.Name}" />                    
    </apex:column>
</apex:pageBlockTable>
Related Topic