+ Start a Discussion
kminevkminev 

Newbie in Visual Force development

Hi,


This is my first simple/mini visual force project I am having some difficulties pass variable back and forth between my visual force page and my control (Apex class)

 

What is the proper approach to pass a var Id from a input field from vforce page to control , then control's logic queies some data and returns the result back to the v force page. It should be very somple pass contact id from vforce page query and return contact name based on the id.

 

Any help will be extremely appreciated.

 

Here is my code as well:

 

VForce Page:

 

<apex:page id="pageForm" controller="ContactLookup">
  <apex:form id="form">
    <apex:inputText id="textFld" value="{!myObject.paramId}" />
    <apex:commandButton value="Lookup" id="btnLookup" rerender="newData"/>
   
    <br/>
    <!--  <apex:outputPanel id="newData">
        Result value: {!myObject.result}<br/>
        Action Result: {!myObject.lookup}<br/>
      </apex:outputPanel>
      -->
  </apex:form>
 
  <!-- <apex:outputPanel id="newData">
    Result value: {!myObject.result}<br/>
    Action Result: {!myObject.lookup}<br/>
  </apex:outputPanel> -->
 
</apex:page>

 

 

My Control Code:

 

 public class ContactLookup {

    public class MyObject {
        
        private String paramId = null;
        private String result = null;
 
    
        public String getParamId() { return paramId; }
        public void setParamId(String param) { paramId = param; }
        
        public String getResult(){return result;}
        public void setResult(String val){result = val;}
    
        public void getLookup() {

          //Contact c = [Select Id, Name from Contact where Id = :paramId];
          result = 'Kiril Minev';
 
        }
      }
    
      private myObject my_object;
    
      public MyObject getMyObject() {
        if (my_object == null) { my_object = new MyObject(); }
        return my_object;
    }
}

 

 

 

Thank you.

 

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

I think here's what you need to change in you VF code:

 

<apex:commandButton value="Lookup" id="btnLookup" rerender="newData" action="{!doStuff}"/>

 

 And here's what you need to add in your ContactLookup controller class:

 

public PageReference doStuff() {

Contact c = [Select Id, Name from Contact where Id = :my_object.paramId];

result = c.Name;

return null;

}

Hope this helps.

 

-soof 

 

 

 

Message Edited by soof on 07-11-2009 01:45 AM