+ Start a Discussion
bittubittu 

I'm trying to get phone value from VF page contact picklist if contact name is selected

I'm trying to get phone value from VF page contact picklist if contact name is selected then phone field of that record need to be display in <apex:inputText title="Phone" />vf page how to do that 

page code:

<apex:page controller="PickList_cls11">
  <apex:form >
      <apex:actionFunction name="change" action="{!dochange}"/>
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:selectList label="Account Names" size="1" onchange="change()" value="{!Aname}" >
                  <apex:selectOptions value="{!options}" >
                  </apex:selectOptions>
              </apex:selectList>
              <apex:selectList label="Contact Names" size="1" value="{!Cname}">
                  <apex:selectOptions value="{!conNames}" >&nbsp;
                  <apex:commandButton value="Show Contact" action="{!Showcon}"/>
                  </apex:selectOptions>
              </apex:selectList>
              <apex:outputText label="Phone">
                  <apex:inputText title="Phone" value="{!cphone}"/>
              </apex:outputText>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

 class;

 

public with sharing class PickList_cls {

    public Contact objb {get; set;}
    public string Cname {get; set;}
    public string Aname {get; set;} 
    public list<SelectOption> options{get; set;}
    public list<selectoption> conNames {get; set;}
    public PickList_cls (){
    objb = new contact();
    options = new List<Selectoption>();
    options.add(new selectoption('--None--','--None--'));
    //for(Account a: [Select id, name, (select id,name from Contacts) from Account order by name]){
    for(Account a: [Select id, name from Account order by name]){
      options.add(new selectOption(a.Id,a.name));
      }
      conNames = new list<selectoption>();
  } 
  public void dochange(){
      conNames.clear();
      conNames.add(new selectoption('Select','Select'));
      for(Contact c:[select id,name from Contact where AccountId=:Aname order by name ])
          conNames.add(new selectoption(c.id,c.name));
      }
    public pagereference Showcon(){
        
        return null;    
    }
    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

I tried your code and you need to add one more query for getting the current selected Contact to show the phone number.

And assign the phone number of that contact to the variable cphone.

 

Try the following. you will get the desired output.

 

public with sharing class PickList_cls {
    public String cphone { get; set; } //to be declared
    public Contact objb {get; set;}
    public string Cname {get; set;}
    public string Aname {get; set;}
    public list<SelectOption> options{get; set;}
    public list<selectoption> conNames {get; set;}
    public PickList_cls (){
    objb = new contact();
    options = new List<Selectoption>();
    options.add(new selectoption('--None--','--None--'));
    //for(Account a: [Select id, name, (select id,name from Contacts) from Account order by name]){
    for(Account a: [Select id, name from Account order by name]){
      options.add(new selectOption(a.Id,a.name));
      }
      conNames = new list<selectoption>();
  }
  public void dochange(){
      conNames.clear();
      conNames.add(new selectoption('Select','Select'));
      for(Contact c:[select id,name from Contact where AccountId=:Aname order by name ])
          conNames.add(new selectoption(c.id,c.name));          
      }
    public pagereference Showcon(){

     //These two lines to be added
        Contact c = [select id,name, phone from contact where id=:Cname limit 1];
        cphone = c.phone;
        return null;    
    }
    
}

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.