4

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?

1
  • I have observed the same behavior as discussed here. May 27, 2015 at 20:57

2 Answers 2

3

I have the solution.

The field for RecordTypeId must be on the page, as an inputField (outputField is not good enough). It can be hidden with styling, but it has to be there.

<apex:inputField value="{!svc.RecordTypeId}" style="display:hidden">
1
  • Thanks.. It worked for me but not sure the reason why it was not working with the Output field. Jul 30, 2013 at 12:39
1

I think your record type gets initialized after the execution of your constructor.

What you can try is write a javascript function in the vf page and call it on window.onload, this javascript function then would call another method - initialize() in your controller extension where you can do the initialization.

2
  • 1
    Thanks for the suggestion Neeraj. Tried that. Didn't work. Same result.
    – user2855
    May 15, 2013 at 15:59
  • This was my solution. My page constructor was creating a new blank which overwrote the recordtype that was selected on the previous page. Thanks codeinprogress
    – Ohmicron
    Mar 16, 2018 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .