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
@isTest
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);
test.startTest();
PageReference pageRef = Page.ActionPage;
test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',l.Id);
ApexPages.StandardController sc = new ApexPages.StandardController(l);
BuildPartnerProducts Controller = new BuildPartnerProducts(sc);
Controller.doSomething();
test.stopTest();
}
}
Extension
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"
extensions="BuildPartnerProducts"
action="{!doSomething}">
</apex:page>
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.
Lead
to the system. Please edit your post to include your test.