You need to sign in to do that
Don't have an account?
Adnan
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>
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
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:
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
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:
Thanks for the help...
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
VF Page:
Sales4ce
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();"
uhh... My Bad.. Pls rename the constructor to this.
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.
I'd rather suggest you to go through the Visual Force documentation.
This is how your controller should look like.
Sales4ce
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;
}
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
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,
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;
}
}
Thanks Ravu... I really Appreciate your solution, but I went with the Workflow rule to update fields.