I have some code in an extension that works fine when it runs on the record, but I'm getting a strange null value on a relationship field when running the test class. This code runs on a button click, not update/insert.

Test Class

Public class BuildPartnerProductsTest {
    public static TestMethod void testthethings(){ 

        Contact c   = new Contact();
        c.LastName  = 'Greene';
        //Other required fields
        insert c;

        Lead l      = new Lead();
        l.LastName  = 'Greene';
        l.Company   = 'Company Name';
        l.Contact__c= c.Id;  //Custom lookup field to the contact
        l.Subject__c= 'Biology';
        insert l;
        system.debug('TestContactID: '+l.Contact__c);


        PageReference pageRef = Page.ActionPage;
        ApexPages.StandardController sc = new ApexPages.StandardController(l);

        BuildPartnerProducts Controller = new BuildPartnerProducts(sc);



public with sharing class BuildPartnerProducts
    public ApexPages.StandardController standardController;
    public BuildPartnerProducts(ApexPages.StandardController standardController)
        this.standardController = standardController;
    public PageReference doSomething()
        Id recordId = standardController.getId();
        Lead l = [SELECT Id, Contact__c, Subject__c FROM Lead WHERE ID=:recordID];
        system.debug('ExtContactID: '+l.Contact__c);

VF Page

<apex:page standardController="Lead"

The debug in the test has the contact ID, but the debug in the Extension is null. It is odd to me that the subject DOES get populated in the extension correctly.

I've tried using Contact__r.ID in the debug and that didn't work. I wasn't able to do it in the query.

  • 1
    That code would break as soon as you add a second Lead to the system. Please edit your post to include your test. – Adrian Larson Oct 13 '17 at 3:04
  • @AdrianLarson Oh yea, I noticed that, updated it in my code, but then forgot to update it here. You want the entire test class? It's pretty big. Or do you mean the part of the test that runs the extension? – denvergreene Oct 13 '17 at 3:14
  • @AdrianLarson I've added everything necessary to reproduce the issue, and then some. I hope it helps. – denvergreene Oct 13 '17 at 3:49
  • 1
    Do you have any triggers clearing this field out by chance? – Adrian Larson Oct 13 '17 at 3:50
  • @AdrianLarson. That was it! I had some other automation that I thought I had bypassed but I actually had not. I definitely should have caught this. Thanks for the reminder. Should I delete this question? It doesn't seem useful to anyone else. – denvergreene Oct 13 '17 at 4:00

The most likely problem here is that you have a trigger clearing out the field. Always double check your trigger logic in cases like this. See also: How do I start to debug my own Apex code?


I had the exact same issue. In my case, I was populating the campaign lookup field on the opportunity. My mistake was that I was setting the field as follows

opportunity.Campaign = campaignRecord;

This would work ok when printing the opportunity object in the debug, but later in the tests it was null.

I should have used the CampaignId as follows

opportunity.CampaignId = campaignRecord.Id;

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.