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
AdnanAdnan 

Default Apex picklist Value

Hi,

 

I am trying to make the default picklist value of LeadSource to "WEB". Can I be assisted?

 

Below is the code.

 

<apex:pageBlockSection title="" collapsible="false" columns="1" >
    <br /><br />
    
    


   
         <apex:inputField value="{!Lead.LeadSource}" />  <----picklist value default to "WEB"              
         
         
         
         <apex:inputfield value="{!Lead.Industry}" required="true"/>     
         <apex:inputField value="{!Lead.Title}"/>
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.FirstName}" required="true"/>
         <apex:inputField value="{!Lead.LastName}"/>         
         <apex:inputField value="{!Lead.Company}"/>
         <apex:inputField value="{!Lead.Street}" required="true"/>
         <apex:inputField value="{!Lead.City}" required="true"/>
         <apex:inputField value="{!Lead.State}" required="true"/>
         <apex:inputField value="{!Lead.PostalCode}" required="true"/>
         <apex:inputField value="{!Lead.Email}" required="true"/>
         <apex:inputField value="{!Lead.Phone}" required="true"/> 
            
       <br /><br />
              </apex:pageBlockSection>

 

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

Try this code and see if it works for you.

 

Public class myWeb2LeadExtension {

 

 

String defaultValue = 'Web';

 

public String getLeadSource(){

            return defaultVale;

}

 

public String setLeadSource(String defaultValue){

          this.defaultValue = defaultValue;

}

 

 

    public myWeb2LeadExtension(ApexPages.StandardController stdController) {
    }
   
     public PageReference saveLead() {
       try {
       insert(Lead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.Thankweb2lead;
       p.setRedirect(true);
       return p;
     }
}

All Answers

sales4cesales4ce

I am assuming that you are using a Standard Controller (Lead) for your VisualForce Page.

If so, you then have to write a Controller Extension( that extends your Lead Standard Controller) to default your Picklist field .

The Controller Extension would have an action method that is called on the pageload which would do the trick.

 

Include the code in red to your Visualforce page.

 

Controller Extension:

public class DefaultValues {
    Private Final Lead ld;
    Private Static Final String LEAD_SOURCE='WEB';
    
    public DefaultValues(ApexPages.StandardController controller) {
        ld = (Lead)Controller.getRecord();
    }
    
    Public PageReference initialize(){
        pageReference pageRef=null;
        if(ld.Id==Null){        
            ld.LeadSource=LEAD_SOURCE;
        }
               
        return pageRef;
    }

}

 

 

Visual Force Page:

 

<apex:Page standardController=”Lead” Extensions=”DefaultValues” action=”{!Initialize}>

 

 

Hope this is of some help.Let me know if it worked for you or not.

 

Sales4ce

AdnanAdnan

Thanks for the reply. I have created the class as you advised. Can you show me exactly where I should put the <apex:Page standardController=”Lead” Extensions=”DefaultValues” action=”{!Initialize}>. Below is my full Page code:

 

 

<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="false">
 <apex:composition template="{!$Site.Template}"> 
  <apex:define name="body"> 
   <apex:form >
    
      
      <apex:pageBlock title="" mode="edit">  



        <apex:pageBlockButtons location="bottom" >
           <apex:commandButton value="Submit" action="{!saveLead}"/>
        </apex:pageBlockButtons>
        
        
       
        
        <apex:pageBlockSection title="To have an Account Representative contact you, please complete and submit the form below or call 877.747.3669" collapsible="false" columns="1" >
        
                   
           <apex:image id="logo" value="{!$Resource.Bresnanphone}" width="300" height="200"/>
    <apex:stylesheet value="{!$Resource.Bresnanphone}"/>

    
   
    
    <apex:pageBlockSection title="" collapsible="false" columns="1" >
    <br /><br />
    
    


   
         <apex:inputField value="{!Lead.LeadSource}" />                
         
         
         
         <apex:inputfield value="{!Lead.Industry}" required="true"/>     
         <apex:inputField value="{!Lead.Title}"/>
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.FirstName}" required="true"/>
         <apex:inputField value="{!Lead.LastName}"/>         
         <apex:inputField value="{!Lead.Company}"/>
         <apex:inputField value="{!Lead.Street}" required="true"/>
         <apex:inputField value="{!Lead.City}" required="true"/>
         <apex:inputField value="{!Lead.State}" required="true"/>
         <apex:inputField value="{!Lead.PostalCode}" required="true"/>
         <apex:inputField value="{!Lead.Email}" required="true"/>
         <apex:inputField value="{!Lead.Phone}" required="true"/> 
            
       <br /><br />
              </apex:pageBlockSection>
                
               
                     </apex:pageBlockSection>      

                 
     
     </apex:pageBlock>
     
   </apex:form>
   
  </apex:define> 
 </apex:composition>  
</apex:page>

 Thanks for the help...

sales4cesales4ce

Aaah...i see that you already have a controller extension defined. No need to define a new one. you can add the code to your extension itself.

Add this method and member variables to your controller extension. updated version of code is in RED.

 

Controller Extension

 

public class myWeb2LeadExtension {
    Private Final Lead ld;
    Private Static Final String LEAD_SOURCE='WEB';
    
    public DefaultValues(ApexPages.StandardController controller) {
        ld = (Lead)Controller.getRecord();
    }
    
    Public PageReference initialize(){
        pageReference pageRef=null;
        if(ld.Id==Null){        
            ld.LeadSource=LEAD_SOURCE;
        }
               
        return pageRef;
    }
// your other logic..
}

 

VF Page:

 

<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="false" action="{!initialize}">
 <apex:composition template="{!$Site.Template}"> 
  <apex:define name="body"> 
   <apex:form >
    
      
      <apex:pageBlock title="" mode="edit">  



        <apex:pageBlockButtons location="bottom" >
           <apex:commandButton value="Submit" action="{!saveLead}"/>
        </apex:pageBlockButtons>
        
        
       
        
        <apex:pageBlockSection title="To have an Account Representative contact you, please complete and submit the form below or call 877.747.3669" collapsible="false" columns="1" >
        
                   
           <apex:image id="logo" value="{!$Resource.Bresnanphone}" width="300" height="200"/>
    <apex:stylesheet value="{!$Resource.Bresnanphone}"/>

    
   
    
    <apex:pageBlockSection title="" collapsible="false" columns="1" >
    <br /><br />
    
    


   
         <apex:inputField value="{!Lead.LeadSource}" />                
         
         
         
         <apex:inputfield value="{!Lead.Industry}" required="true"/>     
         <apex:inputField value="{!Lead.Title}"/>
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.FirstName}" required="true"/>
         <apex:inputField value="{!Lead.LastName}"/>         
         <apex:inputField value="{!Lead.Company}"/>
         <apex:inputField value="{!Lead.Street}" required="true"/>
         <apex:inputField value="{!Lead.City}" required="true"/>
         <apex:inputField value="{!Lead.State}" required="true"/>
         <apex:inputField value="{!Lead.PostalCode}" required="true"/>
         <apex:inputField value="{!Lead.Email}" required="true"/>
         <apex:inputField value="{!Lead.Phone}" required="true"/> 
            
       <br /><br />
              </apex:pageBlockSection>
                
               
                     </apex:pageBlockSection>      

                 
     
     </apex:pageBlock>
     
   </apex:form>
   
  </apex:define> 
 </apex:composition>  
</apex:page>

 

Sales4ce

AdnanAdnan

Sorry to sound lost. I tried to add the code to the extention getting error " Invalid constructor name: DefaultValues at line 5 column 8". Should that be renamed?

 

"public DefaultValues(ApexPages.StandardController controller) {
ld = (Lead)Controller.getRecord();"

sales4cesales4ce

uhh... My Bad.. Pls rename the constructor to this.

 

public myWeb2LeadExtension(ApexPages.StandardController controller) {
        ld = (Lead)Controller.getRecord();
    }
AdnanAdnan

I am still having issues in defining. I am adding my class also. If you can plug it in for in. I know I am asking for too much.

 

public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController stdController) {
       weblead = (Lead)stdController.getRecord();
    }
    
     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.Thankweb2lead;
       p.setRedirect(true);
       return p;
     } 
}

 

 

sales4cesales4ce

I'd rather suggest you to go through the Visual Force documentation.

This is how your controller should look like.

 

 

public class myWeb2LeadExtension {
    private final Lead weblead;
    Private Static Final String LEAD_SOURCE='WEB';
    public myWeb2LeadExtension(ApexPages.StandardController stdController) {
       weblead = (Lead)stdController.getRecord();
    }
    
     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.Thankweb2lead;
       p.setRedirect(true);
       return p;
     } 
Public PageReference initialize(){
        pageReference pageRef=null;
        if(ld.Id==Null){        
            ld.LeadSource=LEAD_SOURCE;
        }
               
        return pageRef;
    }
}

 

 

 

Sales4ce

 

 

sravusravu

In you extension you can use the following code

 

String defaultValue = 'Web';

 

public String getLeadSource(){

            return defaultVale;

}

 

public String setLeadSource(String defaultValue){

          this.defaultValue = defaultValue;

}

AdnanAdnan

Sales4ce,

 

Thank you for all your support. You have provided me with the source. I am now trying to get it in the class.

 

Thanks

Adnan

AdnanAdnan

Hi Sravu,

 

I see you have provided me with the code Thank You!. I would just like to know where in my code do I need to add this. In previous post I have provided my Class code. If you can assist in placing the code in?

 

I appreciate all the help!

 

Thanks,

sravusravu

Try this code and see if it works for you.

 

Public class myWeb2LeadExtension {

 

 

String defaultValue = 'Web';

 

public String getLeadSource(){

            return defaultVale;

}

 

public String setLeadSource(String defaultValue){

          this.defaultValue = defaultValue;

}

 

 

    public myWeb2LeadExtension(ApexPages.StandardController stdController) {
    }
   
     public PageReference saveLead() {
       try {
       insert(Lead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.Thankweb2lead;
       p.setRedirect(true);
       return p;
     }
}

This was selected as the best answer
AdnanAdnan

Thanks Ravu... I really Appreciate your solution, but I went with the Workflow rule to update fields.