+ Start a Discussion

How to set the value of an inputfield on load

Hi All,


I have a quick question and it doesnt appear to have been asked before.


I have a requirement to to set the value of a picklist inputfield value on load. So I can can make three pages. When a user loads page A , I would like a picklist value to be set to A, page b to B and page C to C. This value is then saved into my object and used in a workfow to send emails and the value sets the template.


I suppose you can call this dynamic default values (which would be really cool..).


Anyone have any ideas how this can be done, I dont want to have to creat three extensions which a set value in if I can help it.


Thanks in advance guys




Why can't u make the field value as default in the database itself.....i.e. in the picklist u can make default value and what the value that you defaulted that will display when you bind that to input field....


Thank for the reply Kiran,


I can set one default value fine, but the field is used in a workflow, so I want to be able to change the value of the field depending on the page the user filled in.


So all three pages would use this same field and the three seperate pages would be bound to the default value, this is not what im after.


I want to set that value based on the page completed, I obviously dont mind hard coding the value on the page as I will have the three seperate pages to work with.


I guess i could do this via javascript on load, or via a controller on save, but i would like to be able to display the value on the page if possible.


something along the lines of a read only text box , that has a hard coded value and is still linked to the object field,




This sounds like a prime candidate for the apex:inputHidden component.  This allows input values to be specified in the page but hidden from the user.  The docs for it actually state:


--- snip ---



Use this component to passvariables from page to page.



--- snip ---


Thanks Bob,


This is the route I had to go down , you still have the problem of binding the field to the object and chnageing the value based on the page you land on.


So you can have a blank text field called  "Page_Visited__c" on the object and add it to the visualforce markup as a hidden field, but setting the value of this hidden field was always going to be the tricky bit , remember the only diffirence is the page they landed on.


In the end I had to use Javascript with the OnLoad command to pass a hardcoded variable into the hidden text field and then a workflow to chnage the value of a picklist to that of the hidden value.


If anyone is interested in the markup, then im happy to post.


This could be used really nicely for for marketing purpoes to log the page the user went to or for many other things.


Thanks for your help.


I must admit I skimmed the original requirement a touch.


Could you interrogate the page from the controller when the form is submitted and set the value based on the URL?


A controller (or at leats an entension) was alway an option but that would mean either naming the URL the value of the field I wanted to populate or writting code to resolve it in yhr controller and then se thte value of the field,


When you consider I might want  to use this approach for 6 or 7 pages, continually modifying the controller with IF statements or hardcoded values seemed like far to much work.


Using the javascript approach I can change a hardcoded variable at the top of the page and then pass the value of this variable into the hidden field with out too much pain.


I am thinking of raising an idea to add the abilty to set the default values of picklists when you create a visualforce page. This would be a really nice setting to have when you create a new page. That way you could clone a page and then make it more usable and relvant to that page based the URL alone. Say a vf page was used by a team that did a certain type of work to log their activity and another team needed the same page but did a diffirent type of activities , being able to set the default for each page to the most common activity would be a great feature.


But until then , this is possible through Js and a Hidden inputfield.


Cheers :smileyhappy: