Sunday, August 5, 2012

Lookup Convert TO Picklist:

Lookup Convert TO Picklist:
In this example you can see how to convert your lookup into picklist.You can test with custom object ,the below code is for standard object between contact and account.Contact has lookup field for account object so it converts to picklist .First step is controller looks like this

public class SomeObjectExtension {     private final ApexPages.standardController controller;     private final Contact obj;     public SomeObjectExtension(ApexPages.StandardController stdController) {         this.controller = stdController;         this.obj = (Contact)stdController.getRecord();     }     public SelectOption[] getLocationOptions() {         SelectOption[] locations = new SelectOption[]{};         locations.add(new SelectOption('','--None--'));         for (Account l : [select id, name, AccountNumber, Industry from Account where isdeleted = false order by name]) {             locations.add(new SelectOption(l.id, l.name + ' (' + l.AccountNumber + ', ' + l.Industry + ')'));         }         return locations;     } }
<apex:page standardController="Contact" extensions="SomeObjectExtension" showHeader="false" >
<apex:sectionHeader title="Contact Edit" subtitle="New Some Object" />
<apex:form id="someObjectForm">
<apex:pageBlock title="Contact Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Contact Information" columns="1">
<apex:inputField value="{!Contact.Name}" required="true"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account" for="pLabel"/>
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:actionRegion >
<apex:selectList id="locationLookupPicklist" value="{!Contact.Account}" size="1" rendered="true">
<apex:selectOptions value="{!locationOptions}"/>
</apex:selectList>
</apex:actionRegion>
</apex:outputPanel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
                        

No comments:

Post a Comment