Pre-populate Visualforce Form without a class

The solution below can cause issues when using jquery so I am now using this solution:

Apex: <apex:inputText label=”Customer Number” value=”{!cnumber}” id=”custsearch” styleClass=”form-control”/>

Jquery:
<script>
$(document).ready( function(){
$(“input[name*=’custsearch’]” ).val( “{!cid}” );
setTimeout(function() { $(‘input[name*=”{!focus}”]’).focus() }, 1000);
});
</script>

>> End Update

Something that should be easy is relatively hard to do without busting out apex: Take the values either from the URL or from a form submit and pre-populate / fill in the form fields so that the user does not have to re-enter data we already know or requested from the previous page/interaction.

The solution can be found here: http://salesforce.stackexchange.com/questions/28390/prefill-visualforce-inputfield-from-url-parameter-without-custom-controller

Here is the solution:

<apex:page standardController="Lead">
    <apex:form>
        <apex:inputtext  id="jsName"/>
        <script>
            var jsName = "{!$Component.jsName}";
        </script>

    </apex:form>

    <script>
        window.onload=function()
        {

         document.getElementById(jsName).value = "{!$CurrentPage.parameters.Name}";
        };
    </script>
</apex:page>

I changed up the variables for what I used this for but this example is simple, easy to understand, and (the best part) totally works. Because sometimes you don’t feel like writing and supporting APEX for something that in other platforms is relatively easy to accomplish.

1 Reply to “Pre-populate Visualforce Form without a class”

Leave a Reply

Your email address will not be published. Required fields are marked *