+ Start a Discussion
Kevin Jackson 11Kevin Jackson 11 

Why are my variables not being called from Apex

Why are my variables not being called
I cannot figure out what I am doing wrong here.  This was working before.  

MY VF Page
 
<apex:page Controller="TestP" showHeader="false">

<apex:pageBlock><apex:pageBlockSection>
    <apex:form>
   Email given: {!cemail} 
   Email Returned: {!IdentifyUser.email} 
   Name: {!IdentifyUser.name}
    </apex:form>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>


My Controller
public class TestP {
    Public string cemail {get;set;} 
    Public Contact IdentifyUser {get;set;}
    Public Contact x {get;set;}
    
    // IDENTIFY USER BASED ON EMAIL ADDRESS
    Public Contact getIdentifyUser(){       
     cemail='kevin@caberu.be';   
       Contact x = [SELECT Id, firstname, email, name             
              FROM Contact 
              WHERE email = :cemail 
                              LIMIT 1]; 
        Return x;

    }
}


The VF page does not show any of the variables.

Any help is appreciated.  Where is the bug?
Best Answer chosen by Kevin Jackson 11
Alain CabonAlain Cabon
Hi Kevin,

If you want an input, you can use inputText and a command button "Search".
 
<apex:page Controller="TestP" showHeader="false">
    <apex:pageBlock ><apex:pageBlockSection >
        <apex:form >
            Email given:  <apex:inputText value="{!cemail}"/> <p/>
            <apex:commandButton value="Search" action="{!search}"/>
            Email Returned: {!IdentifyUser.email} 
            Name: {!IdentifyUser.name}
        </apex:form>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>
 
public class TestP {
    Public string cemail {get;set;} 
    Public Contact IdentifyUser {get;set;}
    
    void readContact() {
        IdentifyUser = [SELECT Id, firstname, email, name             
                        FROM Contact 
                        WHERE email = :cemail 
                        LIMIT 1];     
    }
     
    public PageReference search () {
        readContact();   
        return null;
    }
    
    public TestP() {
        // IDENTIFY USER BASED ON EMAIL ADDRES
        cemail='kevin@caberu.be';   
        readContact();
    }
}

return null; means "return to the same page".  This code is basic and should be improved for the errors ('not found') and so on but it works for a test.

Regards
Alain

All Answers

Alain CabonAlain Cabon
Hi,

It just lacks the constructor in you controller if you want some data just calling your VFP without passing parameters.

1. The constructor methods on the associated custom controller or controller extension classes are called, instantiating the controller objects.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_get_request.htm
public class TestP {
    Public string cemail {get;set;} 
    Public Contact IdentifyUser {get;set;}

	public TestP() {
      // IDENTIFY USER BASED ON EMAIL ADDRES
      cemail='kevin@caberu.be';   
      IdentifyUser = [SELECT Id, firstname, email, name             
              FROM Contact 
              WHERE email = :cemail 
             LIMIT 1];    
    }
}
The constructor is  public MyController()  in the sample of Salesforce (no return type):
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm

The syntax for a constructor is similar to a method, but it differs from a method definition in that it never has an explicit return type and it is not inherited by the object created from it.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm)

Regards
Alain
 
Kevin Jackson 11Kevin Jackson 11
Alain,

Great!  This works, but how do I do the same thing with passing parameters?  I want to have cemail as an input and use that to determine the Contact.

cemail=IdentifyUser.email  ??

Kind Regads,
Alain CabonAlain Cabon
Hi Kevin,

If you want an input, you can use inputText and a command button "Search".
 
<apex:page Controller="TestP" showHeader="false">
    <apex:pageBlock ><apex:pageBlockSection >
        <apex:form >
            Email given:  <apex:inputText value="{!cemail}"/> <p/>
            <apex:commandButton value="Search" action="{!search}"/>
            Email Returned: {!IdentifyUser.email} 
            Name: {!IdentifyUser.name}
        </apex:form>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>
 
public class TestP {
    Public string cemail {get;set;} 
    Public Contact IdentifyUser {get;set;}
    
    void readContact() {
        IdentifyUser = [SELECT Id, firstname, email, name             
                        FROM Contact 
                        WHERE email = :cemail 
                        LIMIT 1];     
    }
     
    public PageReference search () {
        readContact();   
        return null;
    }
    
    public TestP() {
        // IDENTIFY USER BASED ON EMAIL ADDRES
        cemail='kevin@caberu.be';   
        readContact();
    }
}

return null; means "return to the same page".  This code is basic and should be improved for the errors ('not found') and so on but it works for a test.

Regards
Alain
This was selected as the best answer
Kevin Jackson 11Kevin Jackson 11
Super!  Thanks!