Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Created February 15, 2012 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbirk/1838114 to your computer and use it in GitHub Desktop.
Save joshbirk/1838114 to your computer and use it in GitHub Desktop.
Dynamic Field Example
public with sharing class TestController {
public String mainContactField {get; set;}
public TestController(ApexPages.StandardController std) {
Contact c = (Contact)std.getRecord();
mainContactField = 'Phone';
List<Contact> cWithRecords = [SELECT Id, Phone, MobilePhone, AssistantPhone, OtherPhone from Contact where ID = :c.Id];
if(cWithRecords.size() > 0) {
if(ApexPages.currentPage().getParameters().get('area') != null) {
if(cWithRecords[0].Phone.replaceAll('[^0-9]+','').startsWith(ApexPages.currentPage().getParameters().get('area'))) {
mainContactField = 'Phone';
}
if(cWithRecords[0].MobilePhone != null && cWithRecords[0].MobilePhone.replaceAll('[^0-9]+','').startsWith(ApexPages.currentPage().getParameters().get('area'))) {
mainContactField = 'MobilePhone';
}
if(cWithRecords[0].AssistantPhone != null && cWithRecords[0].AssistantPhone.replaceAll('[^0-9]+','').startsWith(ApexPages.currentPage().getParameters().get('area'))) {
mainContactField = 'AssistantPhone';
}
if(cWithRecords[0].OtherPhone != null && cWithRecords[0].OtherPhone.replaceAll('[^0-9]+','').startsWith(ApexPages.currentPage().getParameters().get('area'))) {
mainContactField = 'OtherPhone';
}
}
}
//this is just a comment
}
}
<apex:page standardController="Contact" extensions="TestController">
<apex:form >
<apex:messages />
Closest Phone Number: <apex:inputField value="{!Contact[mainContactField]}"/><apex:commandButton action="{!save}" value="Save" />
<apex:outputPanel rendered="false">
<!-- pull in values for std controller query -->
<apex:outputText value="{!Contact.Phone}" />
<apex:outputText value="{!Contact.MobilePhone}" />
<apex:outputText value="{!Contact.AssistantPhone}" />
<apex:outputText value="{!Contact.OtherPhone}" />
</apex:outputPanel>
</apex:form>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment