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
Pritesh Khole 1Pritesh Khole 1 

VF Page on Leads

Hi Team,

On Lead, we have an Accounts as a Look up, we have a requirement that whenver the user selects an Account, the Company name should be populated with the Account Name in Real time, without saving the record. I have created the below VF page and Extension controller, however not able to achieve the result. Any help is appreciated.

Visualforce Page

<apex:page lightningStylesheets="true" standardController="Lead" extensions="LeadControllerExtension" tabStyle="Lead">
    <apex:form id="form1">
        <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">
        </apex:sectionheader>
        <apex:pageblock mode="edit" id="leadPB" title="Lead Edit">
         <apex:pageblockbuttons >
             <apex:commandbutton action="{!save}" value="Save">
             </apex:commandbutton>
          <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
             </apex:pageblockbuttons>
            <apex:pagemessages ></apex:pagemessages>
            <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
            <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
                
            <apex:pageblocksectionitem >
                <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
                <apex:outputpanel >
                <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
                <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
                </apex:outputpanel>
                </apex:pageblocksectionitem>
                <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
                <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
                <apex:inputField value="{!Lead.Company}">
                <!---<apex:actionSupport event="onchange" action="{!getAcc}" reRender="form1"/> -->
                </apex:inputField>
                                                                <apex:inputfield value="{!Lead.Account__c}">
                  <apex:actionSupport event="onchange" action="{!getAcc}" reRender="{!pageBlockList}"/>
                    <apex:messages />
                </apex:inputfield>
                <apex:inputfield value="{!Lead.Email}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
     <apex:inputfield value="{!Lead.Status}"></apex:inputfield>
                
     <apex:pageblocksection id="AddressInformationPBS" title="Address Information">
                <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
                <apex:outputtext value=""></apex:outputtext>
                <apex:inputfield value="{!Lead.City}"></apex:inputfield>
     <apex:outputtext value=""></apex:outputtext>
                <apex:inputfield value="{!Lead.State}"></apex:inputfield>
     <apex:outputtext value=""></apex:outputtext>
                <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
                <apex:outputtext value=""></apex:outputtext>
                <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
                <apex:outputtext value=""></apex:outputtext>
                                                   
      </apex:pageblocksection>
      </apex:pageblocksection>
         
    </apex:pageblock>
  </apex:form>
       
</apex:page>

Extension Controller:

public with sharing class LeadControllerExtension
{
public Lead LR{get;set;}
public Account con{get;set;}
    
public ApexPages.StandardController stdcntrl{get;set;}
public LeadControllerExtension(ApexPages.StandardController cntrl){
stdcntrl=cntrl;
this.LR = (Lead) stdcntrl.getRecord();
}
    
 public string getAcc(){
     //apexpages.addMessage(new ApexPages.message(ApexPages.Severity.INFO, 'Company Value' ));
//Lead new l1=new Lead();
LR=[Select id,name,Account__c,Company from Lead LIMIT 1];
con=[select name from Account where id= :LR.Account__c];

 LR.Company=con.Name;
return LR.Company;

    }

                }

 
Best Answer chosen by Pritesh Khole 1
Raj VakatiRaj Vakati
Use this code
 
<apex:page lightningStylesheets="true" standardController="Lead" extensions="LeadControllerExtension" tabStyle="Lead">
    <apex:form id="form1">
        <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">
        </apex:sectionheader>
        <apex:pageblock mode="edit" id="leadPB" title="Lead Edit" >
            <apex:pageblockbuttons >
                <apex:commandbutton action="{!save}" value="Save">
                </apex:commandbutton>
                <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
            </apex:pageblockbuttons>
            <apex:pagemessages ></apex:pagemessages>
            <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
                <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
                
                
                <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
                <apex:outputpanel >
                    <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
                    <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
                </apex:outputpanel>
                <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
                <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
                
                <apex:inputField value="{!Lead.Company}" id="asdasdasd">
                    <!---<apex:actionSupport event="onchange" action="{!getAcc}" reRender="form1"/> -->
                </apex:inputField>
                <apex:actionRegion >
                    <apex:inputfield value="{!Lead.Account__c}">
                        <apex:actionSupport event="onchange" action="{!getAcc}" reRender="pageBlockList,asdasdasd,democom"/>
                        <apex:messages />
                    </apex:inputfield>
                </apex:actionRegion>
                <apex:inputfield value="{!Lead.Email}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Status}"></apex:inputfield>
                
                <apex:pageblocksection id="AddressInformationPBS" title="Address Information">
                    <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.City}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.State}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    
                </apex:pageblocksection>
            </apex:pageblocksection>
            
        </apex:pageblock>
    </apex:form>
    
</apex:page>

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
<apex:page lightningStylesheets="true" standardController="Lead" extensions="LeadControllerExtension" tabStyle="Lead">
    <apex:form id="form1">
        <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">
        </apex:sectionheader>
        <apex:pageblock mode="edit" id="leadPB" title="Lead Edit" >
            <apex:pageblockbuttons >
                <apex:commandbutton action="{!save}" value="Save">
                </apex:commandbutton>
                <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
            </apex:pageblockbuttons>
            <apex:pagemessages ></apex:pagemessages>
            <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
                <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
                
                
                    <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
                    <apex:outputpanel >
                        <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
                        <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
                    </apex:outputpanel>
                 <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
                <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
                <apex:inputField value="{!Lead.Company}" id="asdasdasd">
                    <!---<apex:actionSupport event="onchange" action="{!getAcc}" reRender="form1"/> -->
                </apex:inputField>
                <apex:inputfield value="{!Lead.Account__c}">
                    <apex:actionSupport event="onchange" action="{!getAcc}" reRender="pageBlockList,asdasdasd,LeadInformationPBS"/>
                    <apex:messages />
                </apex:inputfield>
                <apex:inputfield value="{!Lead.Email}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Status}"></apex:inputfield>
                
                <apex:pageblocksection id="AddressInformationPBS" title="Address Information">
                    <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.City}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.State}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    
                </apex:pageblocksection>
            </apex:pageblocksection>
            
        </apex:pageblock>
    </apex:form>
    
</apex:page>
 
public with sharing class LeadControllerExtension
{
    public Lead LR{get;set;}
    public Account con{get;set;}
    
    public ApexPages.StandardController stdcntrl{get;set;}
    public LeadControllerExtension(ApexPages.StandardController cntrl){
        stdcntrl=cntrl;
        this.LR = (Lead) stdcntrl.getRecord();
    }
    
    public void getAcc(){
        //apexpages.addMessage(new ApexPages.message(ApexPages.Severity.INFO, 'Company Value' ));
        //Lead new l1=new Lead();
       // LR=[Select id,name,Account__c,Company from Lead LIMIT 1];
        con=[select name from Account where id= :LR.Account__c];
        
        LR.Company=con.Name;
      //  return LR.Company;
        
    }
    
}

 
Pritesh Khole 1Pritesh Khole 1
Hi Rej,

I tried the above code, after selecting the account on the look up, the Company name is not populating.

User-added image
Raj VakatiRaj Vakati
Use this code
 
<apex:page lightningStylesheets="true" standardController="Lead" extensions="LeadControllerExtension" tabStyle="Lead">
    <apex:form id="form1">
        <apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">
        </apex:sectionheader>
        <apex:pageblock mode="edit" id="leadPB" title="Lead Edit" >
            <apex:pageblockbuttons >
                <apex:commandbutton action="{!save}" value="Save">
                </apex:commandbutton>
                <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
            </apex:pageblockbuttons>
            <apex:pagemessages ></apex:pagemessages>
            <apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
                <apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
                
                
                <apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
                <apex:outputpanel >
                    <apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
                    <apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
                </apex:outputpanel>
                <apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
                <apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
                
                <apex:inputField value="{!Lead.Company}" id="asdasdasd">
                    <!---<apex:actionSupport event="onchange" action="{!getAcc}" reRender="form1"/> -->
                </apex:inputField>
                <apex:actionRegion >
                    <apex:inputfield value="{!Lead.Account__c}">
                        <apex:actionSupport event="onchange" action="{!getAcc}" reRender="pageBlockList,asdasdasd,democom"/>
                        <apex:messages />
                    </apex:inputfield>
                </apex:actionRegion>
                <apex:inputfield value="{!Lead.Email}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Title}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Website}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
                <apex:inputfield value="{!Lead.Status}"></apex:inputfield>
                
                <apex:pageblocksection id="AddressInformationPBS" title="Address Information">
                    <apex:inputfield value="{!Lead.Street}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.City}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.State}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    <apex:inputfield value="{!Lead.Country}"></apex:inputfield>
                    <apex:outputtext value=""></apex:outputtext>
                    
                </apex:pageblocksection>
            </apex:pageblocksection>
            
        </apex:pageblock>
    </apex:form>
    
</apex:page>

 
This was selected as the best answer
Raj VakatiRaj Vakati
Is its working ?
Pritesh Khole 1Pritesh Khole 1
Hi Raj,

Thanks for the Help. It works. Just a question, I do see that you have used
  <apex:actionRegion >
29                    <apex:inputfield value="{!Lead.Account__c}">
30                        <apex:actionSupport event="onchange" action="{!getAcc}" reRender="pageBlockList,asdasdasd,democom"/>
31                        <apex:messages />
32                    </apex:inputfield>
33                </apex:actionRegion>

May In know why have you added democom to reRender.

Regards
Pritesh
Raj VakatiRaj Vakati
Please remove that .. i Was added while testing .. But as per the code no need . Mark it as solved 
Raj VakatiRaj Vakati
Its it working after removing the value ?
Pritesh Khole 1Pritesh Khole 1
Yes, it is working Raj, Thanks a lot for your help
Raj VakatiRaj Vakati
Mark it as solved !
Pritesh Khole 1Pritesh Khole 1
Marked it as Best Answer, not sure how to change the tag to Solve.