function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
harry.31harry.31 

Apex:inputField not work correctly for lookup field.

I have a very simple VF page, trying to insert a new record, but the Apex:inputField didn't work for lookup field.

 

Following is the sample code:

 

<apex:page controller="contactController">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockTable value="{!contacts}" var="con">
                <apex:column value="{!con.Id}" headerValue="FP #">
                    <apex:facet name="footer">
                        <apex:commandLink value="Add" action="{!add}"/>
                    </apex:facet>
                </apex:column>
                <apex:column value="{!con.AccountId}" headerValue="account">
                    <apex:facet name="footer">
                        <apex:inputField value="{!contact.AccountId}" required="true"/>
                    </apex:facet>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

public class contactController 
{
    public assigntravelrates()
    {
        Contact = new Contact();
        Contacts = [select Id, AccountId from Contact limit 2];
    }
    public List<contact> Contacts { get; set; }
    public Contact Contact { get; set; }
    public void add()
    {
        insert Contact;
    }
}

 

when I first time clicks the lookup icon and clicks the add link, it throws a error says "you must enter a value", then I choose the account again, it throws the "required field missing" error which is correct.

 

 

Did anyone have met the same issue?

miha198206miha198206

Hello,

 

if you will remove required="true" attribute from <apex:inputField value="{!contact.AccountId}" required="true"/> line you will see an error:

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]

 

it means you have to fill Last Name filed before you insert Contact record.

harry.31harry.31

This issue has nothing to do with the required field, I set the required attribute to show the lookup field isn't populated with a value. If the account value is correctly saved,  the page will shows the dml exception other than the "you must enter a value" error.

Maybe it has something to do with the browser, I'll have a try.

harry.31harry.31

Meanwhile, I think the oddest is that if you choose an account and click add link again, the account value is populated correctly...