+ Start a Discussion

InputField only shows default value onload

I'm not sure if this is expected behavior or a bug, or if there is something I'm missing.


As of Winter '11, InputField components will load on your page with the default value for the corresponding field.  Very cool, and it even works for Dynamic Visualforce too.  But I've noticed that it will only work if the field is included at the time the page is loaded.


So if you have anything dynamic going on that causes an inputField to be added to the page such as an action runs and "reRenders" a section causing a new inputField to be rendered within... or if you use Dynamic Visualforce and your controller passes back an inputField that wasn't previously included in the dynamicComponent.  The newly added InputField will be empty, any default value is ignored.


I've even tried to be sneaky and use a wizard approach so that the controller can do some calculating and initalization and interaction with some AJAX on the first page and then when it is ready to display my inputFields it forwards to another page with the same controller.  So the controller and all data remains, but the user is on a fresh page that will display the input form... this didn't work.  So it's clear that the inputField will only have the default if it is part of the page when BOTH the page AND controller (and/or extensions) load.


Bug?  Or is this how it should be?


Of course if the field describe result "getDefaultValue()" method actually did what the name implies then I could just fetch it myself :-)



Salesforce WizardSalesforce Wizard



No idea if this is a bug or not, but I'm curious if this is a work around.


Can you have the field referenced twice? Once as a hidden field and a second that displays in your re-render?


I remember Salesforce supporting some things with a hidden field vs a nonrendered field. For example getting the date picker javascript loaded.


You might try using addFields on the controller in the constructor. I don't know if this will work, but it seems to work correctly when I used it on a dynamic PDF rendering code I tried out recently. The idea is that you just need to get the fields added before getRecord() is called on the Standard Controller, which doesn't happen until an extension explicitly calls it or the end of the extension's constructor method.


@sfdcfox- I'm not sure what you mean by "use getFields", searched the apex docs and didn't find a method that appears to help.  Can you elaborate?



@wizard- in my case, I don't know what fields should be displayed at the time the page loads, it is determined after the fact and a rerender occurs.  But after some experimentation, turns out that even if I did know ahead of time they are treated differently... the inputFields on the page at load will have the default value in them, but the ones added later still would not (unless you had some JavaScript copy from one to the other or some similar hack)