+ Start a Discussion
Kevin Jackson 11Kevin Jackson 11 

Problem passing value from Visual force to APEX

I am having a challenge passing a value selected/entered on a visual force page to the apex controller.  This should just work, but for some reason it is not working.  The value in the controller always reverts to the original value. Can you find the error in my code?
 
<apex:page standardStylesheets="false" controller="testApplyController" showHeader="false" action="{!contactx}">
    
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"></link>
    <meta charset="utf-8" />
    <apex:stylesheet value="{!URLFOR($Resource.customCSS)}"/>

    <title></title>

</head>
<body>
    <div class="ctr">
        <h2 class="title">
           Let's get started.
        </h2>
    </div>
    <br />
    <div class="centerform" >
        <form action="/" method="post"><apex:form >
            <table class="table-main">
                
                <tr>
                    <td style="min-width:270px; padding-right: 1em;">

                        Enter your email address   <br />
                         <apex:inputfield value="{!Contactx.email}" label="email" html-class="form-control" />
                       
                    </td>
                    <td style="min-width:270px; padding-right: 1em;">
                        What is your area of activity? <i class="fa fa-info-circle"  style="color:dimgrey" data-toggle="modal" data-target="#Roles"></i>     <br />
                        <apex:inputfield value="{!Contactx.Area_of_Activity_Signup__c}" label="activity" html-class="form-control"/>
                   		
                    </td>
                    <td style="min-width:270px; padding-right: 1em;">
                        Where do you live? <br />
                      <apex:inputfield value="{!Contactx.MailingCountryCode}" label="Country" html-class="form-control"/> 
                       
                    </td>
                </tr>
                 
            </table>

        </apex:form>  </form>
        <br />
    </div>

    <div class="ctr">
        <Apex:form > <Apex:commandButton styleclass="button back" style="color:teal; margin-right:10px;" action="{!UpdateContact}" value="back"   /> 
                    &nbsp;&nbsp;&nbsp;
        <Apex:commandButton styleclass="button" style="color:white;" action="{!UpdateContact}" value="Update"  /> </Apex:form>
     <!--   <button class="button back" style="color:teal; margin-right:10px;">back</button>

        <button class="button" style="color:white">next</button>
          --> 
    </div>
    <br /><br />
</body>
</html>
</apex:page>
Controller
public class testApplyController {

   	Public Contact Contacty {get;set;}
    Public Contact Contact {get;set;}
    Public Contact Contactx {get;set;}
    public Integer selectedValue{get;set;}
    
    Public  testApplyController(){	    
    }   
       public Contact GetContactx () {   
        Return Contactx;
         }
    public void SetContactx () {   
        system.debug('>>>>>>'+contactx);
        Contactx = contactx;
         }
 
    Public void Contactx(){
        system.debug('---conx---'+contactx);
        If (Contactx ==null){
        String email = 'kevin@caberu.be';
    	Contactx = myutil.getContact(email);       //This does a lookup of a record using myutil.  This works fine.
            } 
        else {
            String email=Contactx.email;
            
       system.debug('-----'+Contactx.Area_of_Activity_Signup__c);
               Contactx = myutil.getContact(email); 
              }            
    } 
    // UPDATE CONTACT INFO
    Public void UpdateContact() { 
        system.debug('---con----'+contactx.Area_of_Activity_Signup__c+contactx.firstname+contactx.MailingCountry);
        Contactx();
        //Contactx= contact;
       System.Debug(contactx.Area_of_Activity_Signup__c);    //Always reverts to the original value
       System.Debug(contactx.firstname); 
       System.Debug(contactx.MailingCountry);  //Always reverts to the original value
        Update Contactx;
        //Apply1();
        //Return null;
    }   
}


 
Best Answer chosen by Kevin Jackson 11
Kevin Jackson 11Kevin Jackson 11
Yes.  The problem was not in the Controller.  It was in the Visual Force Page.

I had <apex:form>  in two places.  One covering the inputs and one covering the button.  Apparently they should both be within the same <apex:form>.   

This is why everyone was stumped.  The code was right, the HTML was not.

Thanks for your input.  :-)

All Answers

RD@SFRD@SF
Hello Kevin,

On the page load action you have {!contactx()}, try changing it to {!Contactx()}.

Hope it solves the issue
RD 
Kevin Jackson 11Kevin Jackson 11
Hi RD.  

No this does not change it.  (by the way, no need for the "()" ).

My issue is not loading the data into the Contactx variable in the first place, the problem is that the value does not change and go back into the variable when I change the value on the Visual Force pge.

ANY OTHER IDEAS OUT THERE?
Dushyant SonwarDushyant Sonwar
Hi Kevin ,
You don't need to declare your method GetContactx and SetContactx as you have already declared your variable as getter;setter
Your SetContactx method is the problem causing the old value to be set when you are changing the values.
Try with below code:
 
public class testApplyController {

   	Public Contact Contacty {get;set;}
    //Public Contact Contact {get;set;}
    Public Contact Contactx {get;set;}
    public Integer selectedValue{get;set;}
    
    Public  testApplyController(){	
    contactY = new Contact();
    contactX = new Contact();
    }   
      /* public Contact GetContactx () {   
        Return Contactx;
         }
    public void SetContactx () {   
        system.debug('>>>>>>'+contactx);
        Contactx = contactx;
         }
 */
    Public void Contactx(){
        system.debug('---conx---'+contactx);
        If (Contactx ==null){
        String email = 'kevin@caberu.be';
    	Contactx = myutil.getContact(email);       //This does a lookup of a record using myutil.  This works fine.
            } 
        else {
            String email=Contactx.email;
            
       system.debug('-----'+Contactx.Area_of_Activity_Signup__c);
               Contactx = myutil.getContact(email); 
              }            
    } 
    // UPDATE CONTACT INFO
    Public void UpdateContact() { 
        system.debug('---con----'+contactx.Area_of_Activity_Signup__c+contactx.firstname+contactx.MailingCountry);
        Contactx();
        //Contactx= contact;
       System.Debug(contactx.Area_of_Activity_Signup__c);    //Always reverts to the original value
       System.Debug(contactx.firstname); 
       System.Debug(contactx.MailingCountry);  //Always reverts to the original value
        Update Contactx;
        //Apply1();
        //Return null;
    }   
}

 
RD@SFRD@SF
Hi Kevin,

Found anything on this yet?

RD 
Kevin Jackson 11Kevin Jackson 11
Yes.  The problem was not in the Controller.  It was in the Visual Force Page.

I had <apex:form>  in two places.  One covering the inputs and one covering the button.  Apparently they should both be within the same <apex:form>.   

This is why everyone was stumped.  The code was right, the HTML was not.

Thanks for your input.  :-)
This was selected as the best answer