+ Start a Discussion
RossGRossG 

dynamic dependent picklist issue

Hi,

I've overridden the standard "New" button functionality on the Lead object using a visualforce page and custom controller.

The purpose of this override is to allow for a dynamic dependent picklist when creating a new lead.  Specifically, the requirement is: when a user selects a value from the standard lead picklist field called "Lead Source"...then...show a second custom field picklist called "Lead Source Detail".  The values that the user sees in this second picklist must only be the names of Campaigns in SFDC that have a value in their Type field equal to the value the user selected in the Lead Source field.

Here's an example.

User clicks "New" button on the lead tab to create a new lead in the ui.

User sees the Lead Source picklist field.  Let's say it has values of A,B,C.  User selects A.

The user should then have the option of selecting a value in a second picklist field called Lead Source Detail.  The options the user should see are only the campaigns in SFDC that have a Type field value of "A".  So for example, say there are 2 campaigns in SFDC with a Type field value of "A".  These would therefore be the only 2 selectable options in the Lead Source Detail field for the user.

The following code works for setting up a couple of dependent picklists on a lead New override screen, but, it's not quite there.  I'm not sure how to modify it to get what I want above.

If you create a couple custom objects Category__c, Feature__c, and put a picklist field on Lead object called "Category__c", and put in the following 2 classes and VF page, and use the vf page to override the New button on Lead object, you'll see where I am.  This all works to override the lead new button with a couple dyamic dependent picklists, but I need to modify this code so that, basically, the dependent picklist fields are:

1. the controlling field should be the lead.leadsource field and
2. the dependent picklist field should display a list of campaigns where the campaign type = the lead source value selected in option 1

the vf page: 

<apex:page standardController="Lead" extensions="LeadNew_ControllerExtension2" title="Lead Edit" tabStyle="Lead">

   <apex:form id="form">
        <apex:pageBlock title="Lead Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!doSave}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Lead Information">
                
                <apex:pageBlockSectionItem >
                <apex:outputlabel value="First Name"></apex:outputlabel>
                <apex:inputfield value="{!Lead.FirstName}"/>               
                </apex:pageBlockSectionItem>                
                
                <apex:pageBlockSectionItem >
                <apex:outputlabel value="Last Name"></apex:outputlabel>
                <apex:inputfield value="{!Lead.LastName}"/>               
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                <apex:outputlabel value="Company"></apex:outputlabel>
                <apex:inputfield value="{!Lead.Company}"/>               
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                <apex:outputlabel value="Email"></apex:outputlabel>
                <apex:inputfield value="{!Lead.Email}"/>               
                </apex:pageBlockSectionItem>              
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel for="productList" value="{!$ObjectType.Lead.fields.Product__c.label}" />
                    <apex:actionRegion >
                        <apex:selectList value="{!product}" title="Product" size="1" id="products">
                            <apex:selectOptions value="{!productList}" />
                            <apex:actionSupport event="onchange" rerender="versions" />
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel for="versions" value="{!$ObjectType.Lead.fields.Version__c.label}" />
                    <apex:actionRegion >
                        <apex:selectList value="{!version}" title="Version" size="1" id="versions">
                            <apex:selectOptions value="{!versionList}" />
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>

</apex:page>
the controller:

global with sharing class LeadNew_ControllerExtension2 {
    private final Id recordId;
    private final Lead record;
    private final ApexPages.StandardController controller;
 
    public LeadNew_ControllerExtension2(ApexPages.StandardController stdController) {
        this.controller = stdController;
        this.recordId = this.controller.getId();
        this.record = [
            select Id, ProductText__c,Product__c,Version__c,Product__r.Name,LeadSource,Lead_Source_Detail_Text__c,
                LastName,Email,Company,
                VersionText__c
            from Lead WHERE Product__c = null
         //   where Id = :this.recordId
            limit 1
        ];
        
    }
 
    public List<SelectOption> getProductList() {
        List<SelectOption> products = new List<SelectOption>();
        products.add(new SelectOption('', '--None--'));
 
        for (Product__c p: ProductUtils2.getAllProducts()) {
            products.add(new SelectOption(p.Id, p.Name));
        }
 
        return products;
    }
 
    public Id getProduct() {
        return this.record.Product__c;
    }
 
    public void setProduct(Id productId) {
        this.record.Product__c = productId;
    }
 
    public List<SelectOption> getVersionList() {
        List<SelectOption> versions = new List<SelectOption>();
        versions.add(new SelectOption('', '--None--'));
 
        if (record.Product__c != null) {
            for (Version__c v: ProductUtils2.getAllVersions(getProduct())) {
                versions.add(new SelectOption(v.Id, v.Name));
            }
        }
 
        return versions;
    }
 
    public Id getVersion() {
        return this.record.Version__c;
    }
 
    public void setVersion(Id versionId) {
        this.record.Version__c= versionId;
    }
 
    public PageReference doSave() {
        Lead c = (Lead) controller.getRecord();

        c.Product__c = this.record.Product__c;
        c.Lead_Source_Detail_Text__c = this.record.Version__c;
        
        upsert c;
 
        return new PageReference('/'+c.Id);
    }
}

util class used in the controller:

public with sharing class ProductUtils2 {
    
    static public List<Product__c> getAllProducts(Boolean includeEOL) { 
        return [
            select Name
            from Product__c
            order by Name
        ];
    }
 
    public static List<Product__c> getAllProducts() {
        return getAllProducts(false);
    }
 
    public static List<Version__c> getAllVersions(Id productId, Boolean test) {
 
        return [
            select Name,
                Product__c
            from Version__c
            WHERE Product__c =: productId
            order by Name
        ];
    }
 
    public static List<Version__c> getAllVersions(Id productId) {
        return getAllVersions(productId, false);
    }
}



Naveen IlaNaveen Ila
You need to place lead source in the page and tag an action ( probably actionSupport/actionFunction) to populate Lead Source Detail field.