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
Chirag Sapkota 5Chirag Sapkota 5 

How to auto populate field values on lead record from Users or Contact records while creating a new lead via VF page form?

Hello developer gurus,
I have create a VF page form to capture leads for our community users. The form is StandradController "Lead" and TabStyle "Lead". I would like this form to be more advance. Below is the fuctionality I would like to create. I am newbee to the Apex.
1) Whenever a user (specially, Community User) access this form, the form should auto populate the User's FirstName, LastName, Email, Comapny, Account (info will be in Contact or User detail record)

Thank you in advance. Again, I would appreciate if you could provide me some codes 
ArvindyadavArvindyadav
Hi Chirag,

for your problem You have to create an extension(leadExtension) where you have to set the values you want to pre-populate. please see below code snippet. and let me know if you were looking for something like this. if yes hit like. Thanks! 

 
<apex:page standardController="lead" showHeader="true" sidebar="true" extensions="leadExtension">
    <apex:sectionHeader title="New Lead"/>
    <apex:form id="pageForm">
        <apex:pageBlock title="Lead Details">
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!save}" id="saveButton"/>
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
            <br />
            <apex:pageBlockSection title="Owner" columns="2" collapsible="false">
                <apex:inputText value="{!firstName}" label="First Name" />
                <apex:inputText value="{!lastName}" label="Last Name" />
                <apex:inputText value="{!email}" label="Email" />
                </apex:pageBlockSection>
                </apex:pageBlock>
    </apex:form>
</apex:page>


public class leadExtension {

    public String firstName {get;set;}
    public String lastName {get;set;}
    public String email {get;set;}
        
    public leadExtension(ApexPages.StandardController controller) {
    
        firstName = UserInfo.getFirstname();
        lastName = UserInfo.getLastname();
        email = UserInfo.getUserEmail();
    }

}

 
Chirag Sapkota 5Chirag Sapkota 5
Hello Arvind, Thank you so much for the response, however, I need to add more fields, both custom and Standard on my VF page form. When I start with CompanyName the page gives me following error

"Error: Could not resolve the entity from <apex:inputField> value binding '{!CompanyName}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable."

Please advise.
Thank you
 
ArvindyadavArvindyadav
Hi Chirag,

You can set the values in extension's constructor if the field exist and use it as apex:inputfield in visualforce page. see the below updated code as per your requirment.
 
<apex:page standardController="lead" showHeader="true" sidebar="true" extensions="leadExtension">
    <apex:sectionHeader title="New Lead"/>
    <apex:form id="pageForm">
        <apex:pageBlock title="Lead Details">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" id="saveButton"/>
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
            <br />
            <apex:pageBlockSection title="Owner" columns="2" collapsible="false">
                <apex:inputfield value="{!lead.firstName}" label="First Name" />
                <apex:inputfield value="{!lead.lastName}" label="Last Name" />
                <apex:inputfield value="{!lead.email}" label="Email" />
                <apex:inputfield value="{!lead.company__c}" label="company" />
                </apex:pageBlockSection>
                </apex:pageBlock>
    </apex:form>
</apex:page>


public class leadExtension {    
    public String email {get;set;}
    private final Lead ld;
    public leadExtension(ApexPages.StandardController sc) {    
        this.ld = (Lead)sc.getRecord();
        ld.firstname = userinfo.getFirstName();
        ld.lastname = userinfo.getLastName();
        ld.email = userinfo.getUserEmail();
        //Get the company name for now i have given a hardcoded value
        ld.company__c = 'CompanyName';        

    }

}
and let me know if it worked for you.

 
Chirag Sapkota 5Chirag Sapkota 5
Hello Arvind,
Thank you for the amend code. However, the atupopulation only work as for the record owner.
I using the form in our community portal via iframe. And, when community user access this form Their Name, Email and Account should autopopulate. This is not the case with the above code. Right now, it only auto populate the info of the form Creator eventhough I login as a community user and  access the form. Please advise