function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AMAN SINGH 24AMAN SINGH 24 

hello all, I am using action support to fill the phone number from account but it is not working

<apex:page standardController="Contact" extensions="V_sg1" >
  <apex:form >
    <apex:pageBlock >
       <apex:pageBlockSection >
             <apex:inputField value="{!Contact.Lastname}" />
                
             <apex:inputField value="{!Contact.AccountID}" >
              <apex:actionSupport event="OnChange" action="{!phone}" />
                 </apex:inputField>
             <apex:inputField value="{!Contact.phone}" />
             <apex:inputField value="{!Contact.Fax}" />
      </apex:pageBlockSection>
      
    </apex:pageBlock>
  </apex:form>
</apex:page>


public class V_sg1 {

   

   public contact con;
    public V_sg1(ApexPages.StandardController controller) {
     con=(contact)controller.getRecord();
       

    }
     public PageReference phone() {
    Account acc=[select id,phone from Account where id=:con.AccountID];
    con.phone=acc.phone;
        return null;
    }


    
      }
DeveloperSudDeveloperSud
Try This below code 
<apex:page standardController="Contact" extensions="V_sg1" >
  <apex:form >
    <apex:pageBlock >
       <apex:pageBlockSection >
             <apex:inputField value="{!Contact.Lastname}" />
                
             <apex:inputField value="{!Contact.AccountID}" >
              <apex:actionSupport event="onchange" action="{!phone}" reRender="ip1" /> 
                 </apex:inputField>
             <apex:inputField value="{!Contact.phone}"  id="ip1" />
             <apex:inputField value="{!Contact.Fax}" />
      </apex:pageBlockSection>
      
    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller class is same and dont forget to pass the id of the contact in the url like '?id=contactid'.
AMAN SINGH 24AMAN SINGH 24
It is not working and Please explain why to pass an id to URL, i want when Account is selected from lookup then phone automatically come in the box from account phone.  
DeveloperSudDeveloperSud
You need to pass the contact id from the url otherwise it wont work based on the code written in your extension class.If you dont pass any id in the url the variable "con " inside your extension class will have a null value and so that the query written inside phone method will return null.Pass a contact id in url .The solution has been given based on the login in your extension class only.let me know if you still having problem.
AMAN SINGH 24AMAN SINGH 24
Please give me extension code such that I choose the account name from lookup and automatically the phone number is displayed in phone field.
 thanks in advance
DeveloperSudDeveloperSud
Hi Aman,

I think you donot want to pass the id from url, if that the case you can refer the below code for your requirement. The page has been created like that it will give the user the chance to create a new contact .The user can choose the account he wants to add and phone no field will automatically update with the choosen account's phone no.The fax field I have left for user choice.
 
<apex:page controller="sample2"  >

 <apex:form id="f" >
 <apex:actionFunction name="newjs" reRender="ip2" action="{!showPhone}"  >
  <apex:param name="newActName" assignTo="{!newActName}" value="" />
  </apex:actionFunction>
  <apex:pageBlock mode="maindetail" id="pb1">
   <apex:outputLabel value="Contact Name" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <apex:inputText value="{!Name}"/>
   </apex:outputlabel>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <apex:outputLabel value="Account Name" id="op1" > &nbsp;
   <apex:inputfield value="{!con.accountID}" onchange="actionJs()" id="ip1" /><br/>
   </apex:outputlabel><br/>
   <apex:outputLabel value="Account's Phone" id="ip2"> &nbsp;
   <apex:inputfield value="{!con.account.phone}"/>
   </apex:outputlabel>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <apex:outputLabel value="Contact's Fax" > &nbsp;&nbsp;&nbsp;
   <apex:inputText value="{!Fax}"/><br/>
   </apex:outputlabel>
  </apex:pageBlock>
  <apex:commandButton value="Save" action="{!save}"/>
 </apex:form>
 
 <script>
   function actionJs(){
   
   
   var valueOfActName=document.getElementById('{!$Component.f.pb1.ip1}').value;
   newjs(valueOfActName);
   }
 
 
 </script>
 
</apex:page>
 
public with sharing class sample2 {
 
 public String name{get;set;}
 public String fax{get;set;}
 public Contact con{get;set;}
 public list<Account> act{get;set;}

 public String newActName{get;set;}
 
   public sample2(){
   
   con=[select name,accountID,fax,account.phone from contact LIMIT 1];
   
   }
   
   
   public pagereference showPhone(){
   
   Account ac=[select phone from account where name=:newActName ];
   String newPhoneNo=ac.phone;
   con.account.phone=newPhoneNo;
   return null;
   
   }

   public pagereference save(){
   
   contact c=new contact();
   c.lastName=name;
   c.fax=fax;
   c.accountID=con.accountID;
   c.phone=con.account.phone;
   insert c;
   
   return null;
   
   }
  


}