+ Start a Discussion
Sam WardSam Ward 

How can I pull a the value entered from an input field on a VF page to the controller?

Here is my Class
global class AdditionalUnitRequest {  
    
    string RecId = apexpages.currentpage().getparameters().get('id');
    
    public string fieldvalue {get;set;}
    public decimal yourNoUnits {get;set;}
    public Opportunity opp {get;set;}
    
    // This gets the finance details related to RecId
         
    list <Finance_Details__c> FinanceDetails;
     global List <Finance_Details__c> getFinanceDetails(){
         FinanceDetails = [SELECT id,Billing_Cycle__c,Account_Name__r.Name,Term__c,Price_Per_Month__c,Contact__r.Name
            FROM Finance_Details__c WHERE Account_Name__c = :RecId AND RecordType.Name = :'Last Order Details' Limit 1];
     return FinanceDetails;
     }
    
    // This is for the picklist options
    
	public string selectedname{get;set;}
    
            
    global list <selectOption> getselectednamefields(){
      list<selectOption> lstnamesel = new list <selectOption>();
            lstnamesel.add(new selectOption('','-- None --'));
            for (Contact con: [SELECT id,name,email FROM Contact WHERE Account.id =: '001D0000026gSLH' AND Allowed_to_Sign__c =: TRUE])
            lstnamesel.add(new selectOption(con.id,con.name));
            return lstnamesel;
        }
    

    // This creates the opportunity with details
    
    public void passValueToController(){
        fieldvalue = selectedname;
        
        string RecId = apexpages.currentpage().getparameters().get('id');        
        string RecType = '012D0000000Bamx';
        string OppType = 'Additional Units';
        string Stage = '02 - Pending';
        string SubStage = 'Finance Proposed';
        string TM = '005D0000001ynPu';
        string OppCur = 'CAD - Canadian Dollar';
        string HowsFunded = 'external Lease (Canada)';
    	String Contact = fieldvalue;
        
        Contact c = [SELECT id, name, email, Account.Name FROM Contact WHERE contact.id =: fieldvalue];
       
        string CompanyName = c.Account.Name;
  
        // Assign values to opp
        opp = New Opportunity();
        opp.Name = CompanyName + ' 1';
        opp.AccountId = RecId;
        opp.Main_Contact_Name__c = Contact;
        opp.StageName = Stage;
        opp.Type = OppType;
        opp.Sub_Stage__c = SubStage;
        opp.CloseDate = system.today() + 31;
        opp.User__c = id.valueOf(TM);
    //opp.TotalOpportunityQuantity = ; ****** I'm wanting to update this field from the VF Page *********
        opp.CurrencyIsoCode = 'CAD';
        opp.How_is_the_order_funded__c = HowsFunded;
        
        
        //Create Opp
        insert opp;
    } 

}

Here is my VF Page: 
 
<apex:page showHeader="false" sidebar="false" Controller="AdditionalUnitRequest" >

<style>
</style>

    
    <apex:dataTable value="{!FinanceDetails}" var="fd" width="700px">
        <apex:column headerValue="Billing Cycle" width="12%">
            <apex:outputText value="{!fd.Billing_Cycle__c}"/> 
        </apex:column>
        <apex:column headerValue="Account Name" width="12%">
            <apex:outputText value="{!fd.Account_Name__r.Name}"/> 
        </apex:column>
        <apex:column headerValue="Term" width="12%">
            <apex:outputText value="{!fd.Term__c}"/> 
        </apex:column>
        <apex:column headerValue="Price Per Month" width="12%">
            <apex:outputText value="{!fd.Price_Per_Month__c}"/> 
        </apex:column>
        <apex:column headerValue="Contact" width="12%">
            <apex:outputText value="{!fd.Contact__r.Name}"/>
        </apex:column>
    
    </apex:dataTable>

<apex:form >
    <span> Contacts: </span>
        <apex:selectList size="1" value="{!selectedname}" onclick="" >
            <apex:selectOptions value="{!selectednamefields}"/>
           <!--   <apex:actionSupport action="{!passValueToController}" reRender="values" event="onchange"/>   -->
        </apex:selectList>
       <apex:inputField value="{!Opp.TotalOpportunityQuantity}" >
       </apex:inputField>
       <!-- <apex:outputText value="{!fieldValue}" label="You have selected:" id="values"/> -->
        <apex:commandButton status="sending" styleClass="buttons" action="{!passValueToController}" title="Order Additionals" value="Order Additionals" oncomplete="Submitted()">
        </apex:commandButton> 
        
</apex:form>



    
</apex:page>

It is the total Opportunity Quantity I'm trying to to map into the the class, I have tried various things but I can't seem to figure it out. I'm sure its something really obvious i'm missing any help would be appreciated. Thanks in advance. 
Soyab HussainSoyab Hussain
Hi Sam Ward,

I go through your issue, You just have to add your class constructor and reallocate your opportunity object. 

You can use directly this code this will help you.
 
global class AdditionalUnitRequest {  
    
    string RecId = apexpages.currentpage().getparameters().get('id');
    
    public string fieldvalue {get;set;}
    public decimal yourNoUnits {get;set;}
    public Opportunity opp {get;set;}
    
    public AdditionalUnitRequest() { // You have to allocate memory for your opportunity object
        opp = new Opportunity();
    }
    // This gets the finance details related to RecId
    
    list <Finance_Details__c> FinanceDetails;
    global List <Finance_Details__c> getFinanceDetails(){
        FinanceDetails = [SELECT id,Billing_Cycle__c,Account_Name__r.Name,Term__c,Price_Per_Month__c,Contact__r.Name
                          FROM Finance_Details__c WHERE Account_Name__c = :RecId AND RecordType.Name = :'Last Order Details' Limit 1];
        return FinanceDetails;
    }
    
    // This is for the picklist options
    
    public string selectedname{get;set;}
    
    
    global list <selectOption> getselectednamefields(){
        list<selectOption> lstnamesel = new list <selectOption>();
        lstnamesel.add(new selectOption('','-- None --'));
        for (Contact con: [SELECT id,name,email FROM Contact WHERE Account.id =: '001D0000026gSLH' AND Allowed_to_Sign__c =: TRUE])
            lstnamesel.add(new selectOption(con.id,con.name));
        return lstnamesel;
    }
    
    
    // This creates the opportunity with details
    
    public void passValueToController(){
        fieldvalue = selectedname;
        
        string RecId = apexpages.currentpage().getparameters().get('id');        
        string RecType = '012D0000000Bamx';
        string OppType = 'Additional Units';
        string Stage = '02 - Pending';
        string SubStage = 'Finance Proposed';
        string TM = '005D0000001ynPu';
        string OppCur = 'CAD - Canadian Dollar';
        string HowsFunded = 'external Lease (Canada)';
        String Contact = fieldvalue;
        
        Contact c = [SELECT id, name, email, Account.Name FROM Contact WHERE contact.id =: fieldvalue];
        
        string CompanyName = c.Account.Name;
        
        // Assign values to opp
        opp = New Opportunity();
        opp.Name = CompanyName + ' 1';
        opp.AccountId = RecId;
        opp.Main_Contact_Name__c = Contact;
        opp.StageName = Stage;
        opp.Type = OppType;
        opp.Sub_Stage__c = SubStage;
        opp.CloseDate = system.today() + 31;
        opp.User__c = id.valueOf(TM);
        opp.TotalOpportunityQuantity = opp.TotalOpportunityQuantity;  
        opp.CurrencyIsoCode = 'CAD';
        opp.How_is_the_order_funded__c = HowsFunded;
        
        //Create Opp
        insert opp;
    } 
    
}
LinkedIn: https://www.linkedin.com/in/soyab-hussain-b380b1194/ 

Regards,
Soyab
 
Ajay K DubediAjay K Dubedi
Hi Sam,

I have understood your requirement and got an example related to you question.
Please review below the example and apply the changes in your codes accordingly.

Controller--
public class AccountExtension {
  /**
   * The variable that stores the Account details
   *
   * @var Account
   */

  private final Account record;

  /**
   * Contructor. Takes a standard controller variable and assigns it to the record
   *
   * @param ApexPages.StandardController stdController
   */

  public AccountExtension(ApexPages.StandardController stdController) {
    this.record = (Account)stdController.getRecord();
  }
}

VF Page--
<apex:page showHeader="true" sidebar="true" standardController="Account" extensions="AccountExtension">
  <apex:form >
    <apex:pageBlock id="container" title="Account Form">
      <apex:pageBlockButtons>
        <apex:commandButton action="{!Save}" value="Save" reRender="errors" status="loading" />
        <apex:commandButton action="{!Cancel}" value="Cancel" reRender="errors" status="loading" />
        <apex:actionStatus id="loading">
          <apex:facet name="start">
            <img src="/img/loading.gif" />
          </apex:facet>
        </apex:actionStatus>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Information">
        <apex:inputField value="{!Account.Name}" />
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

If you want to access posted values in your extension, you can simply reference the record variable. For example, if you created a Save() method that would override the standard Save() method in your extension:
 
public PageReference Save() {
  String accountName = record.Name;
}
Or, as per your markup you're using a method called changeStatus:

public PageReference changeStatus() {
   String accountName = record.Name;
}

Here also I got a link , you can refer this also for more help.

https://salesforce.stackexchange.com/questions/184257/get-values-from-visualforce-page-to-controller



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 
Deepali KulshresthaDeepali Kulshrestha
Hi Sam,

You can assign a variable to store the value of your input field in the controller and reference that variable as the value of that input field like this:

<apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!yourInputFieldVariable}"/>
</apex:pageBlockSection>

You controller will look like this:

public class yourController {
  public String yourInputFieldVariable {get;set;}

  public void yourMethod() {
    System.debug('This is your input field value' + yourInputFieldVariable);
  }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com