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
Gunners_23Gunners_23 

Passing value from VF page to controller

HI all,

I have created a VF page which is having account standard controller and an extension, please find the below code

 

<apex:page standardController="Account" sidebar="false" showHeader="false" extensions="Report">
<apex:form id="thisForm" >

<apex:inputField value="{!account.CustomerPriority__c}" /><br/><br/>
<apex:inputField value="{!account.SLASerialNumber__c}"/><br/><br/>


<apex:commandButton value="Search" action="https://ap1.salesforce.com/00O90000002Gcja?getSearch()"/>


</apex:form>
</apex:page>

 

 

Here is the extension class,

 

public class Report {




public Report(ApexPages.StandardController controller) {


}

Public String getSearch()
{

// Here i want to return a string which will be address but that address should contain

the values passed from the page

 

I tried to access using this {!account.CustomerPriority__c}, but its not working


}

}

 

Please help me out ASAP

 

Thanks & Regards,

bob_buzzardbob_buzzard

You won't be able to have a getter that reacts to the input fields in that way, as getters are called when the page is first rendered.

 

It looks to me like you should be invoking an actionfunction in the controller that builds the URL and returns it.  Something like

 

Page:

<apex:page standardController="Account" sidebar="false" showHeader="false" extensions="Report">
<apex:form id="thisForm" >
<apex:inputField value="{!account.CustomerPriority__c}" /><br/><br/>
<apex:inputField value="{!account.SLASerialNumber__c}"/><br/><br/>

<apex:commandButton value="Search" action="{!search}"/>


</apex:form>
</apex:page>

 controller:

 

public class Report {



public Report(ApexPages.StandardController controller) {


}

Public PageReference search()
{
   PageReference result=new PageReference('https://ap1.salesforce.com/00O90000002Gcja'); result.getParameters().put('priority=', account.CustomerPriority__c); return result;
}
}
 

 

Gunners_23Gunners_23

HI BOB,

Thanks for replying, could you please help me out with this thing

 

The exact thing i want is to pass these two parameters in the form to controller and build a URL based on these two parameters. Could you Pls explain me how to retrieve these two parameter values and how to build URL and pass as a String or Page Reference

 

 

 

bob_buzzardbob_buzzard

The code I've posted above should do this.

 

The "parameters" you are referring to are actually input fields on the form, thus you have to submit the form to the controller in order to be able to access them.  Once your action method is invoked, those fields will be populated in the record the controller is managing.

 

You can then build the URL as a page reference and return that to the browser.

Gunners_23Gunners_23

HI Bob,

I tried using your code its not working, its throwing error like getParameter function will return Map<String,String>

 

The functionality i need to do it in controller is something like this

 

public class Report {

 

Public String getSearch()
{

return '/{!account.CustomerPriority__c}&{!account.SLASerialNumber__c}';


}

}

 

I need to build URL by passing those two values and return to the VF page

 

Thanks in advance,
 

hemantgarghemantgarg

Hi ,

 

You can get the parameters by this way :-

 

 

<apex:page standardController="Account" sidebar="false" id="pg" showHeader="false" extensions="Report">
<apex:form id="thisForm" >

<apex:inputField value="{!account.CustomerPriority__c}"  id="f1" /><br/><br/>
<apex:inputField value="{!account.SLASerialNumber__c}" id="f2"/><br/><br/>


<apex:commandButton value="Search" action="https://ap1.salesforce.com/00O90000002Gcja?getSearch()"/>


</apex:form>
</apex:page>


Public String getSearch()
{

 string f1 = ApexPages.currentPage().getParameters().get('pg:thisForm:f1');
 string f2 = ApexPages.currentPage().getParameters().get('pg:thisForm:f2');

}


 

 

Please verify the exact id of these parameters from the actual vf page by inspecting the generated html of vf page.

 

 

bob_buzzardbob_buzzard

You can't get the parameters this way, as the getter will run as part of the page being rendered.  If you want to capture what the user has entered server side, you must submit the page back to the controller.

 

The other way would be to use javascript to pull the contents of the input fields from the page and build the URL, and fire all that off via an onclick handler.

 

 

 

hemantgarghemantgarg

Hi bob_buzzard,

 

It works , I have tested following code on my dev org :-

 

 

Controller :-


public class TestAccController{
    public Account testAcc{get;set;}
    
    public TestAccController(){
        testAcc = new Account();
    }
    
    public void insertAcc(){
    
        String nm = ApexPages.currentPage().getParameters().get('pg:frm:accName');
        System.debug('=========='+nm);
    }
    
    Public String getSearch(){

        String nm = ApexPages.currentPage().getParameters().get('pg:frm:accName');
        System.debug('=========='+nm);
        return nm;
    }
}


Page :-


<apex:page controller="TestAccController" id="pg">
  <apex:form id="frm">
      <apex:actionFunction name="submitPartial" action="{!insertAcc}" rerender="testPnl"/>
      Name : <apex:inputField id="accName" value="{!testAcc.Name}" />
      <div onclick="submitPartial();">Click Here </div>
      <apex:commandButton value="Search" action="https://ap1.salesforce.com/00O90000002Gcja?{!Search}"/>
  </apex:form>
</apex:page>

 

 Please try it ..

 

 

 

bob_buzzardbob_buzzard

This isn't the code that you posted above though.  As I have said a number of times, you have to submit the form.  In your latest code you are submitting the code via an actionfunction.  

 

As you are doing that, why would you pull the parameters from the URL rather than using the fields from the sobject that are bound to the apex:inputfield components?  Not only is it fragile, in that the ids will change if the structure of the page changes, but this is also bad practice from a security point of view, as they leave your code open to Cross Site Request Forgery, as documented in the Visualforce Developer's Guide.

 

Its much better to let Visualforce work as intended - capture the information into the sobject exposed as a controller property, then provide an action method that produces a page reference based on that input.

hemantgarghemantgarg

Thanks for sharing the information bob_buzzard , I know the code I posted above is not the real , t I just wrote that on notepad to give an example. Also I agree that it is not the good practice.

But I have a doubt that action function does submit the form or not, I think Action function does not submit the whole form, while command button does. and that is the difference between an action fucntion and command button action. If the form is submitted means your setter functions for the inputs written in between form tags will get called and you can get them by their respective property name(like acc.name or so..) in the controller.  And before form submission you can get their values by there element ids. Please correct me if I am wrong.

bob_buzzardbob_buzzard

Actionfunction submits the whole form in the same way that a command button does - the postback still occurs in the same way. You should only need to access values by id in javascript on the page, not server side.

 

If you only wanted to submit part of a form, you would use an actionregion, which only submits the particular input fields inside the actionregion tags.