1

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;

After initialising pro as:

ts2__Project_Job__c pro = new ts2__Project_Job__c();

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

  • Try this one my be __r will be issue : pro.Candidate__r.id = candidate.Id; – Anu Jul 24 '15 at 9:37
  • Thank you for the suggestion, but that hasn't worked unfortunately. I should clarify doing it this way, I'm getting the debug log error: "System.NullPointerException: Attempt to de-reference a null object". – Dan Jones Jul 24 '15 at 9:54
  • I think you have to create new Candidate__c and contact records seprately and then attach them to each other in your save method logic. – Pranay Jaiswal Jul 24 '15 at 10:07
  • When you have initialized pro? – San Jul 24 '15 at 10:18
  • @San pro is initialised just before "pro.Candidate__c = contactLookup;" using "ts2__Project_Job__c pro = new ts2__Project_Job__c();" for instance. – Dan Jones Jul 24 '15 at 10:20
3

This assignment will always fail:

pro.Candidate__c = contactLookup.Id;

Any field of type Id has to have the correct Id sObject type assigned to it.

Here's what I see:

  • pro instance of ts2__Project_Job__c (although you don't show the instantiation, we'll just go with that assumption).
  • pro.Candidate__c relationship field to candidate data type is Id of Candidate__c sObject type.
  • contactLookup instance of ts2_Project_Job__c
  • contactLookup.Id Id of ts2_Project_Job__c sObject type.

Whatever you do, you need to be assigning like for like. Going back to your code:

pro.Candidate__c = contactLookup.Id;

Here you are trying to assign the ts2_Project_Job__c Id value (primary key) to a relationship field that is expecting a Candidate__c value.

You need to assign a Candidate__c id value.

So that could be:

pro.Candidate__c = contactLookup.Candidate__c;

Or it could be a candidate variable from somewhere else:

Candidate__c candidateVariable = getCandidateInstanceFromSomewhere();

pro.Candidate__c = candidateVariable.Id;

It's hard to know without seeing more of your code. But I still get myself twisted around from time to time.

If you have an hour to just learn about how this all works, check out our webinar from September 2014 that did a deep dive on this topic.

  • Thank you so much Peter, this has been haunting me for a while now! I had no idea that Id's were also associated with SObjects. I tried converting the contactLookup to a String (worked, Id was there), to an Id (worked, showed in the debug logs), only then did I get the error regarding Id object types. Great explanation though. Thank you! – Dan Jones Jul 24 '15 at 11:22
  • Sure...we did a webinar related to this about a year ago. I've added the link to the end of my answer. – pchittum Jul 24 '15 at 12:51
1

You should be using

pro.Candidate__c = contactLookup.Candidate__c;

Both pro and contactLookup are instances of ts2__Project_Job__c object. So when you got the contact in Candidate__c of contactLookup instance you can assign the same to Candidate__c of pro instance.

Hope it helps.

  • 1
    Thank you Vigneshwaran. This worked exactly as I needed! Only reason I've given the selected answer to Peter is because of his explanation. But, again, you're bang on with this. Thank you for looking at it! – Dan Jones Jul 24 '15 at 11:24

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.