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
PFangPFang 

Using URL parameters or query strings to update web form field values

Hi Everyone,

 

Great day!

 

I have this issue with using URL parameters to update field values on our web form as unfortunately the variations of codes I was able to research on didn't get that functionality to work. What we're basically trying to do here is use a URL parameter to prepopulate the account field on the VF page. So far, this is what I was able to formulate:

 

public with sharing class myQuestionnaireExtensionII {
    private final Questionnaires__c webquestionnaire;
    public myQuestionnaireExtensionII(ApexPages.StandardController
    stdController) {
        webquestionnaire = (Questionnaires__c) stdController.getRecord();
    }
    // it creates an empty record for the variable. 
    public Questionnaires__c getQuestionnaire() {
        if (webquestionnaire == null) Questionnaires__c webquestionnaire = new Questionnaires__c();
        webquestionnaire.Account__c = ApexPages.currentPage().getParameters().get('accname');
        return webquestionnaire;
    }
    public PageReference saveQuestionnaire() {
        String accname = ApexPages.currentPage().getParameters().get('Account__c');
        String acname = ApexPages.currentPage().getParameters().get('Questionnaires__c.Account__c');
        if (accname == null) {
            // Display the Visualforce page's content if no Account Name is passed over
            return null;
        }
        if (acname == null) {
            // Display the Visualforce page's content if no Account Name is passed over
            return null;
        }
        try {
            insert(webquestionnaire);
            return null;
        } catch (System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference p = Page.ThankYou;
        p.setRedirect(true);
        return p;
    }
}

 Here's the URL for our web form:

 

http://apex/samplequestionnairebrendandevtest

 

Here are some of the things I've tried with the class I'm working on:

 

1.) The getmethod()

 

 // it creates an empty record for the variable. 
    public Questionnaires__c getQuestionnaire() {
        if (webquestionnaire == null) Questionnaires__c webquestionnaire = new Questionnaires__c();
        webquestionnaire.Account__c = ApexPages.currentPage().getParameters().get('accname');
        return webquestionnaire;

 

which was based from this code I picked up from our boards:

 

public Opportunity getOpportunity() {
      if(opportunity == null) opportunity = new Opportunity();
      opportunity.recordtypeid = ApexPages.currentPage().getParameters().get('RecordType');
      opportunity.AccountID = ApexPages.currentPage().getParameters().get('accid');
      opportunity.Name = ApexPages.currentPage().getParameters().get('oppName');
      opportunity.StageName = ApexPages.currentPage().getParameters().get('Stage');
      return opportunity;
   }

 

2.) Other references lead me to doing this:

this.questionnaire = (Questionnaires__c)stdcontroller.getRecord();
        string actname = System.currentPageReference().getParameters().get('accname');

 

3.) Assigned page parameters to strings:

public PageReference saveQuestionnaire() {
        String accname = ApexPages.currentPage().getParameters().get('Account__c');
        String acname = ApexPages.currentPage().getParameters().get('Questionnaires__c.Account__c');
        if (accname == null) {
            // Display the Visualforce page's content if no Account Name is passed over
            return null;
        }
        if (acname == null) {
            // Display the Visualforce page's content if no Account Name is passed over
            return null;

 

4.) I even went as far as putting all these lines into the class:

 

public with sharing class myQuestionnaireExtensionII {
        Questionnaires__c questionnaire = new Questionnaires__c();          
        private final Questionnaires__c webquestionnaire;
        public myQuestionnaireExtensionII(ApexPages.StandardController
        stdController) {        
        string accountname = System.currentPageReference().getParameters().get('accname');                               
        webquestionnaire = (Questionnaires__c)stdController.getRecord();
        String aname = ApexPages.currentPage().getParameters().get('aname');
        String acname = ApexPages.currentPage().getParameters().get('Questionnaires__c.Account__c'); 
        String accname = System.currentPageReference().getParameters().get('Questionnaires__c.Account__c');                        
        }                     

    // it creates an empty record for the variable. 

 

 

5.) I've also tried these lines for our VF page:

<c:SetValue from="{!$CurrentPage.parameters.Account__c}" to="{!Questionnaires__c.Account__c}"/>
<apex:component >
    <apex:attribute name="from" type="String" description="The account name to be set"/>
    <apex:attribute name="to" type="String" description="The variable for setting the account info into"/>
</apex:component>

 

Note: I was hoping that through query strings in our URL, we'd be able to update our web form as shown here - http://http://apex/samplequestionnairebrendandevtest?accname=test (prefill account name field with 'test' value), which doesn't seem to be doing anything. 

 

Burned 48 hours for this and it still feels like nowhere, so any assistance would be superlatively appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
PFangPFang

Ignore my post, got it resolved thanks to the man, Bob_buzzard!

 

Here's how it worked, didn't need to add anything into my apex class, just created this Apex Component into the Setup | Develop path followed by the line below:

 

<apex:component >
    <apex:attribute name="from" type="String" assignTo="{!to}" description="The value to set"/>
    <apex:attribute name="to" type="String" description="The variable to set the value into"/>    
</apex:component>

 

VF page line:

 

<c:SetValue2 from="{!$CurrentPage.parameters.Account__c}" to="{!Questionnaires__c.Account__c}" />

 

Bob, you saved me gazillion of development hours, now where's that smileyveryhappy emoticon...

 

Reference:

http://boards.developerforce.com/t5/Visualforce-Development/Query-string-VisualForce-for-Professional-Edition/m-p/469215/highlight/true#M52942