+ Start a Discussion
michaelforcemichaelforce 

lookup inputField causing errors w/ standard controller + extension

Check out this strange behavior I have encountered.  The basic scenario is that there is a VF page created with standard controller (for our example, Contact object) and then you extend the controller and in the extension you create an object of a different type (in this example we create an opportunity).

 

Then you add a simple form to the page to capture a lookup (in this example we'll have an input field for the Account of the Opportunity we've defined).  Lastly, create a button that will basically do nothing (just refresh the page).

 

Renders ok, but when you lookup an Account, then hit the button you get a strange "An error occurred when processing your submitted information." error message on the input field.  What is the deal?  Does anyone have any suggestions on how to make this work?

 

 

page:

 

<apex:page standardController="Contact" extensions="MyExtension"> <apex:form> <apex:inputField value="{!myOpp.Account}"/> <apex:commandButton action="doSomething" value="Do Something"/> </apex:form> </apex:page>

 

extension:

 

public class MyExtension { public Opportunity myOpp {get;set;} public MyExtension(ApexPages.StandardController controller) { myOpp = new Opportunity(); } public PageReference doSomething(){ return null; } }

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

Thanks for including a succinct repro case. The issue is that you are binding to the relationship and not to the actual reference field. Change this:

 

<apex:inputField value="{!myOpp.Account}"/>

 

to this:

 

<apex:inputField value="{!myOpp.AccountId}"/>

 

 

Also, your action attribute value is a literal which is not going to invoke your extension method as I presume you expect.

 

I'll make sure we add better compile time validation to keep you from hitting that unhelpful error.

 

Thanks,

Andrew

 

All Answers

mtbclimbermtbclimber

Thanks for including a succinct repro case. The issue is that you are binding to the relationship and not to the actual reference field. Change this:

 

<apex:inputField value="{!myOpp.Account}"/>

 

to this:

 

<apex:inputField value="{!myOpp.AccountId}"/>

 

 

Also, your action attribute value is a literal which is not going to invoke your extension method as I presume you expect.

 

I'll make sure we add better compile time validation to keep you from hitting that unhelpful error.

 

Thanks,

Andrew

 

This was selected as the best answer
michaelforcemichaelforce

So strange... I was pretty sure I tried that... but I guess not, because it works!

 

Cheers, Andrew

Rohan62003Rohan62003

Hello,

 

I have the same case but here I have this VF page on my force.com site and trying to get account value using  lookup on contact object.

 

Not getting any issue while accessing it from within salesforce but facing "Authorization Required" if I access from site.

 

Any insight please!!

 

below is Class code:

 

 

public class CreateContact { public String FirstName {get; set;} public String LastName {get; set;} public String Phone {get; set;} public String birth {get; set;} public String AccountName {get; set;} public String Email {get; set;} public CreateContact(ApexPages.StandardController stdController) { }

 

and following is VF page:

 

<apex:page StandardController="Contact" Extensions="CreateContact" sidebar="false" showHeader="false" tabStyle="Contact"> <apex:form > <apex:pageBlock title="New Contact Entry"> <p> AccountName:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <apex:inputField value="{!contact.AccountId}"/></p> <p><apex:commandButton action="{!saveRecords}" value="Save New Contact"/></p> </apex:pageBlock> </apex:form> </apex:page>