I have a VF page, a standard controller and a custom controller extension working with a custom object called Services__c
.
In the controller extension Constructor, I have the following code (this is abbreviated):
ctl=controller;
svc=(Services__c)ctl.getRecord();
//Get and set the record type id.
RecordType rType = [Select Id from RecordType where sObjectType = 'Services__c' and DeveloperName = 'Internal' limit 1];
svc.RecordTypeId=rType.id;
The rest of the constructor sets various field values on svc
, all of which appear when the page loads. The point is, the record type is set before the page is loaded and is then visible on the form (this shows the correct record type).
<apex:outputField value="{!Services__c.RecordTypeId}" />
One of the fields on the VF page (Operated_By__c
) is a related object lookup with a filter. The filter restricts the lookup selection values to a subset of related objects based on creiteria that use the record type of the current record (being pedenatic--the filter refers to the record type by name, not RecordTypeId
, of course), and the value of another field further up the form (Locality__c
).
<apex:pageBlockSection columns="1">
<apex:inputField value="{!Services__c.service_type__c}"/>
<apex:inputField value="{!Services__c.Locality__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection columns="2">
<apex:inputField value="{!Services__c.Default_Location__c}" />
<apex:inputField required="true" value="{!Services__c.Operated_By__c}" />
</apex:pageBlockSection>
This works fine in the standard page layout (when creating a new record through the standard interface) in both creating and editing Services__c
records. The lookup values in the selection for Operated By are correctly filtered to include only the relevant organisations.
However, when I switch to the VF page, the selection list in the Operated_By__c
field is empty when first creating a new record - no options are presented. It only works for re-editing a record that has already been saved to the database.
My suspicion is that the record type is not being properly initialised for the VF page for a new record.
Any suggestions as to what might be going on here?