1

I am trying to pass a number of parameters to a form using javascript and was successful following the post here:

Prefill visualforce inputfield from URL parameter without custom controller

However, the same code does not work to pre-populate the picklists and checkboxes. Could anyone help me understand what changes to make?

The code is:

<script>
                    var ContactFirstName = "{!$Component.ContactFirstName}";
                    var ContactLastName = "{!$Component.ContactLastName}";
                    var Phone = "{!$Component.Phone}";
                    var Email = "{!$Component.Email}";
                    var BusinessName = "{!$Component.BusinessName}";
                    var AnnualRevenue = "{!$Component.AnnualRevenue}";
                    var Province = "{!$Component.Province}";
                    var NumberOfPolicies = "{!$Component.NumberOfPolicies}";
                    var BTaxHistory = "{!$Component.BTaxHistory}";
                    var BTaxDetails = "{!$Component.BTaxDetails}";
                    var PTaxHistory = "{!$Component.PTaxHistory}";
                    var PTaxDetails = "{!$Component.PTaxDetails}";
                    var InsuranceHistory = "{!$Component.InsuranceHistory}";
                    var insuranceRiskHistory = "{!$Component.insuranceRiskHistory}";
                    var businesscheck = "{!$Component.businesscheck}";
          </script>
       </apex:form>
        <script>
                window.onload=function()
                {

                 document.getElementById(ContactFirstName).value = "{!$CurrentPage.parameters.fname}";
                 document.getElementById(ContactLastName).value = "{!$CurrentPage.parameters.lname}";
                 document.getElementById(Phone).value = "{!$CurrentPage.parameters.tel}";
                 document.getElementById(Email).value = "{!$CurrentPage.parameters.email}";
                 document.getElementById(BusinessName).value = "{!$CurrentPage.parameters.busname}";
                 document.getElementById(AnnualRevenue).value = "{!$CurrentPage.parameters.annrev}";
                 document.getElementById(Province).value = "{!$CurrentPage.parameters.prov}";
                 document.getElementById(NumberOfPolicies).value = "{!$CurrentPage.parameters.numppol}";
                 document.getElementById(BTaxHistory).value = "{!$CurrentPage.parameters.bthis}";
                 document.getElementById(BTaxDetails).value = "{!$CurrentPage.parameters.btdets}";
                 document.getElementById(PTaxHistory).value = "{!$CurrentPage.parameters.pthis}";
                 document.getElementById(PTaxDetails).value = "{!$CurrentPage.parameters.ptdets}";
                 document.getElementById(InsuranceHistory).value = "{!$CurrentPage.parameters.inshis}";
                 document.getElementById(insuranceRiskHistory).value = "{!$CurrentPage.parameters.insdets}";
                 document.getElementById(businesscheck).value = "{!$CurrentPage.parameters.buscov}";

                };
        </script>

and the URL looks something like: /apex/Web_Form_to_DG?fname=XXX&lname=XXX&tel=XXX&email=XXX&busname=XXX&annrev=PICKLISTVALUE&prov=XXX&numppol=PICKLISTVALUE&bthis=PICKLISTVALUE&btdets=details&pthis=PICKLISTVALUE&ptdets=details&inshis=PICKLISTVALUE&insdets=details&buscov=TRUE

1

You can not set the value of a checkbox by simply setting the value. Setting this element is slightly different. I have amended your code, try that out:

<script>
                var ContactFirstName = "{!$Component.ContactFirstName}";
                var ContactLastName = "{!$Component.ContactLastName}";
                var Phone = "{!$Component.Phone}";
                var Email = "{!$Component.Email}";
                var BusinessName = "{!$Component.BusinessName}";
                var AnnualRevenue = "{!$Component.AnnualRevenue}";
                var Province = "{!$Component.Province}";
                var NumberOfPolicies = "{!$Component.NumberOfPolicies}";
                var BTaxHistory = "{!$Component.BTaxHistory}";
                var BTaxDetails = "{!$Component.BTaxDetails}";
                var PTaxHistory = "{!$Component.PTaxHistory}";
                var PTaxDetails = "{!$Component.PTaxDetails}";
                var InsuranceHistory = "{!$Component.InsuranceHistory}";
                var insuranceRiskHistory = "{!$Component.insuranceRiskHistory}";
                var businesscheck = "{!$Component.businesscheck}";
      </script>
   </apex:form>
    <script>
            window.onload=function()
            {

             document.getElementById(ContactFirstName).value = "{!$CurrentPage.parameters.fname}";
             document.getElementById(ContactLastName).value = "{!$CurrentPage.parameters.lname}";
             document.getElementById(Phone).value = "{!$CurrentPage.parameters.tel}";
             document.getElementById(Email).value = "{!$CurrentPage.parameters.email}";
             document.getElementById(BusinessName).value = "{!$CurrentPage.parameters.busname}";
             document.getElementById(AnnualRevenue).value = "{!$CurrentPage.parameters.annrev}";
             document.getElementById(Province).value = "{!$CurrentPage.parameters.prov}";
             document.getElementById(NumberOfPolicies).value = "{!$CurrentPage.parameters.numppol}";
             document.getElementById(BTaxHistory).value = "{!$CurrentPage.parameters.bthis}";
             document.getElementById(BTaxDetails).value = "{!$CurrentPage.parameters.btdets}";
             document.getElementById(PTaxHistory).value = "{!$CurrentPage.parameters.pthis}";
             document.getElementById(PTaxDetails).value = "{!$CurrentPage.parameters.ptdets}";
             document.getElementById(InsuranceHistory).value = "{!$CurrentPage.parameters.inshis}";
             document.getElementById(insuranceRiskHistory).value = "{!$CurrentPage.parameters.insdets}";
             if("{!$CurrentPage.parameters.buscov}" == 'TRUE'){                 
                 document.getElementById(businesscheck).checked = true;
             }else{
                 document.getElementById(businesscheck).checked = false;
             }

Although setting a picklist in Javascript is as simple as setting the value so I do not know why this would not work. If you continue to have issues I highly recommend uploading the jQuery library as a static resource and including that in your page. The jQuery library is much easier to use than Javascript and there is a specific function of setting select list values.

  • Thanks for your help. I made the changes you suggest but it still didn't work. I have uploaded the jQuery library as you suggested. Can you give an example of how the code will work or suggest a resource? – Nichelle Dec 31 '14 at 13:53
  • Firstly ensure to include the jQuery library you have uploaded in the header of the Visualforce page. Google to find the jQuery shortcodes that you need. As an example, instead of document.getElementById(PTaxHistory).value = "{!$CurrentPage.parameters.pthis}"; You would write: $("#PTaxHistory").val("{!$CurrentPage.parameters.pthis}"); – David Dawson Jan 2 '15 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.