You need to sign in to do that
Don't have an account?
Pritesh 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;
}
}
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;
}
}
All Answers
I tried the above code, after selecting the account on the look up, the Company name is not populating.
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