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
Dovid BDovid B 

default values in VF input fields

Since when using apex:inputfield, inputtext, inputhidden, etc. the value attribute is bound to the apex or sobject property, is there any way to specify default values in the form, like a checked checkbox or a default string in the text box, etc?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Hmm.  Given that you don't want conditionals in the controller, I assume you would also not want to have them in a factory style utility class that generated an appropriately pre-filled object based on the page reference?  

 

That being the case, I think that JavaScript may well be the route to go.

All Answers

bob_buzzardbob_buzzard

I think this will depend on what controller type you are using.  If you are simply using the standard controller, you won't be able to specify defaults when creating a new record as the controller will instantiate a new (empty) record to back the various input fields.  If you can use an extension or custom controller, you can specify the defaults either in the extension constructor (by retrieving the record from the parent standard controller and modifying the fields) or in the custom controller constructor when creating an sobject or properties.

Dovid BDovid B

Thanks for the quick response Bob,

 

I am using a custom controller and I had thought of that. However the controller is used for a few different versions of the same page, just each one has a different hidden checkbox that's checked. So I don't want to have to build a different controller for each page. Nor do I want to add a bunch of conditionals in the controller.

 

I suppose I could use some js to set the field client side.

bob_buzzardbob_buzzard

Hmm.  Given that you don't want conditionals in the controller, I assume you would also not want to have them in a factory style utility class that generated an appropriately pre-filled object based on the page reference?  

 

That being the case, I think that JavaScript may well be the route to go.

This was selected as the best answer