Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active October 7, 2015 00:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/3077272 to your computer and use it in GitHub Desktop.
Save pcon/3077272 to your computer and use it in GitHub Desktop.
Dynamic dependent picklists
<apex:page standardController="Case" extensions="CaseEdit_ControllerExtension" title="Case Edit" tabStyle="Case">
<apex:form id="form">
<apex:pageBlock title="Case Edit">
<apex:pageBlockButtons>
<apex:commandButton action="{!doSave}" value="Save" />
<apex:commandButton action="{!cancel}" value="Cancel" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Case Information">
<apex:inputField value="{!Case.Summary}" required="true" />
<apex:pageBlockSectionItem>
<apex:outputLabel for="productList" value="{!$ObjectType.Case.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.Case.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>
global with sharing class CaseEdit_ControllerExtension {
private final Id recordId;
private final Case record;
private final ApexPages.StandardController controller;
public Case_ControllerExtension(ApexPages.StandardController stdController) {
this.controller = stdController;
this.recordId = this.controller.getId();
this.record = [
select Product__c,
Subject,
Version__c
from Case
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: ProductUtils.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: ProductUtils.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() {
Case c = (Case) controller.getRecord();
c.Product__c = this.record.Product__c;
c.Version__c = this.record.Version__c;
upsert c;
return new PageReference('/'+c.Id);
}
}
public with sharing class ProductUtils {
static public List<Product__c> getAllProducts(Boolean includeEOL) {
//This is done since the formula field cannot return a boolean
Integer currentlySupported = (includeEOL) ? 0 : 1;
return [
select Name
from Product__c
where Currently_Supported__c >= :currentlySupported
order by Name
];
}
public static List<Product__c> getAllProducts() {
return getAllProducts(false);
}
public static List<Version__c> getAllVersions(Id productId, Boolean includeEOL) {
Integer currentlySupported = (includeEOL) ? 0 : 1;
return [
select Name,
Product__c
from Version__c
where Currently_Supported__c >= :currentlySupported and
Product__c = :productId
order by Name
];
}
public static List<Version__c> getAllVersions(Id productId) {
return getAllVersions(productId, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment