I've got the following APEX:

public Contact accountLookup {

And in the constructor:

accountLookup = new Contact();

With the following on the Visualforce page:

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

This looks at the Account field on the Contact object and generates a Lookup ability to the Account object.

Where this works, I'm struggling to figure out how to write a test for this.

In my test class I'm creating an Account as follows:

Account a = new Account(
    Name = 'My Company'


And want to put that generated account in the public Contact accountLookup variable. The problem is that when I try to do this, I'm getting the error:

Illegal assignment from Account to Contact

With controller.accountLookup = a; and the error:

Illegal assignment from Id to Contact

with controller.accountLookup = a.Id;

I understand I've created a Contact object in order to generate the lookup, but I'm unsure of exactly how to make the Account I've generated work for said Contact object.


I think you need to this

controller.accountLookup.AccountId = a.Id;

You need to assign account id and then continue with your flow in the code.

  • Perfect! I knew it'd be super simple but just couldn't quite wrap my head around what I was missing. Thank you. – Dan Jones Nov 19 '15 at 13:00
  • Absolutely, in precisely 7 minutes! ;) – Dan Jones Nov 19 '15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.