I am overriding the 'New' button on the WorkOrder standard object with a Visualforce page. My users will be creating their WorkOrder records from the Case object. I want to get the CaseId using the StandardController addFields() method in the page constructor.
This is the controller code:
public class TestCon {
public WorkOrder workRecord {get; set;}
public TestCon(ApexPages.StandardController stdCon) {
stdCon.addFields(new List<String>{'CaseId'});
this.workRecord = (WorkOrder)stdCon.getRecord();
System.debug('workRecord: ' + workRecord);
}
}
An empty 'workRecord' is returned in the log and workRecord.CaseId returns null.
When I use the same code for a different object, e.g. overriding the Case New button and returning ContactId the code works as expected and ContactId is available to me in the controller extension.
EDIT:
This code for a Case controller extension does return the ContactId, I can't see any material difference myself.
public class TestCon {
public Case caseRecord {get; set;}
public TestCon(ApexPages.StandardController stdCon) {
stdCon.addFields(new List<String>{'ContactId'});
this.caseRecord = (Case)stdCon.getRecord();
System.debug('caseRecord: ' + caseRecord);
}
}