Skip to main content
2 of 4
added 190 characters in body
Dan Jones
  • 7.3k
  • 4
  • 41
  • 63

Can't Pass Through Value of Lookup Field on Insert

I'm having trouble passing through the value of a Lookup field on Insert. I've got an object called ts2__Project_Job__c, which has a custom Lookup field called Candidate__c which looks at the Contact standard object. In my Application I simply want to use this Lookup field, to find a contact, then Insert a new record.

Here's what I've got:

VisualForce

<apex:inputField id="candidate" value="{!contactLookup.Candidate__c}" />

Apex

This is used to get the value:

public ts2__Project_Job__c contactLookup {
  get;
  set;
}

This is used to instantiate:

contactLookup = new ts2__Project_Job__c();

So far, so good. It creates the Lookup field on the VF Page and it works great! However, when trying to Insert using the following code:

pro.Candidate__c = candidate;

I get the error:

Illegal assignment from ts2__Project_Job__c to Id

I've tried using

pro.Candidate__c = candidate.Id;

Which will save and create the record, but won't populate the field. Debug logs show that this is blank, whereas the variable candidate does have an Id in there. It's just the wrong type. I've tried using:

Id candidate = String.valueOf(contactLookup);

And passing this through, but nothing happens again.

Kind of stuck on this one!

Thanks!

Edit: When using:

pro.Candidate__c = contactLookup.Id;

I'm getting the following in the debug logs:

System.NullPointerException: Attempt to de-reference a null object

Dan Jones
  • 7.3k
  • 4
  • 41
  • 63