function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nothingisreal68nothingisreal68 

Picklist values not rendering default values when object is instantiated AFTER page load.

In the dev docs, it states:

 

"Beginning with API version 20.0, an inputField matched to a field with a default value has the default value prepopulated on the Visualforce page."

 

This functionality works fine when the object is instantiated in the constructor on page load. However, I need to develop a page that renders different objects on a certain part of the page based on the click of a button by the user. When I instantiate this object in a controller action method from the user's click, the default value does NOT appear. Is this standard functionality? I am using an <apex:inputField /> as the document describes. 

 

Thanks in advance for the help!

Chamil MadusankaChamil Madusanka

Hi,

 

If you can please share your code.

Shashikant SharmaShashikant Sharma

Could you please share you code.

nothingisreal68nothingisreal68

I wrote up a very quick example to illustrate what I am trying to describe:

 

Visualforce Page:

 

 

<apex:page controller="testController" >   
    <apex:form >           
        <apex:pageBlock id="theBlock" >           
            <apex:pageBlockButtons >               
                <apex:commandButton value="Render Block" action="{!reRenderBlock}" reRender="theBlock"/>           
            </apex:pageBlockButtons>           
            <apex:pageBlockSection >                               
                <apex:inputField value="{!plan.Days_Dispensed_Mail_Order__c}"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

 

 

Apex Controller:

 

 

public with sharing class testController {
    public pg_Protocol_Plan__c plan { get; set; }       
 
    public void reRenderBlock() {       
        plan = new pg_Protocol_Plan__c();       
    }       
 
    public TestController() {       
        plan = new pg_Protocol_Plan__c();     
    }
}

 

 

 

The custom field "Days_Dispensed_Mail_Order__c" is a picklist with several values, and the default set to "90".

 

When the page first loads, the picklist displays "90", as it should, because it is the default value. However, after you click reRenderBlock to rerender the page and instantiate a new instance of the pg_Protocol_Guide__c object "plan", the picklist displays the value "--None--", which is incorrect. 

 

It seems that when the visualforce page is first loaded, it defaults all picklist values to their correct defaults. BUT, if you instantiate the object AFTER the page first loads, it doesn't not respect the defaults. Does this make sense?

 



 

 

 

 

 

 

 

Chamil MadusankaChamil Madusanka
public with sharing class testController {
    public pg_Protocol_Plan__c plan { get; set; }       
 
    public void reRenderBlock() {       
        plan = new pg_Protocol_Plan__c();   
plan.Days_Dispensed_Mail_Order__c = '90'; } public TestController() { plan = new pg_Protocol_Plan__c(); } }

try this changes in reRernderBlobk method.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

nothingisreal68nothingisreal68

Chamil,

 

I appreciate the quick reponse. I understand that is a solution but it is not what I am looking for. The problem is, though it will default the value, this value is being set in code. If an end user ever wants to change this value, they would have to edit and redeploy code which is simply not acceptable for this project. The goal is to design the system to require as little developer maintanance as possible in the future, meaning I would want the users to change this default value through declarative setup.

 

Are defaults only being set on pageload simply a limit of apex and visualforce that I need to deal with? Or am I overlooking something else?

rohitsfdcrohitsfdc

yeah, i think thats a bug in the system. only way to get the default value is to refresh the page on button click.

 

nothingisreal68nothingisreal68

Rohit,

 

Actually, it seems that even if you refresh the entire page, the default value still doesn't come through. It ONLY works when the page is first loaded.

rohitsfdcrohitsfdc

hello,

if you want the default value on refreshing the page, change your controller action method to the following code:

public PageReference reRenderBlock() {       
        plan = new new pg_Protocol_Plan__c();      
        PageReference p=new PageReference('/apex/testController2_page');
        p.setRedirect(true);
        return p;
    }     

 

nothingisreal68nothingisreal68

Rohit,

 

Thats not really rerendering the page, that seems like a hard redirect, causing the entire page to be regenerated from SFDC. Any values that were previously entered would all be lost as the page is created from scratch, illustrating again that picklists only default when the page is first loaded, right? I meant that even reRendering the entire page by not including a "reRender" attribute on the <apex:CommandButton /> doesn't populate picklists with defaults.

teemosaksteemosaks

You are correct. Picklists only default when the page is first loaded making the Winter 11 announcement incomplete in some respect. This is a bug and I reckon should be posted on Ideas.

 

I would vote for this!