There was an unique requirement of the client she wants a custom lookup to be shown on the visualforce page, So I have created a custom lookup for here with same functionality as Salesforce standard lookup, In this lookup we are populating some standard objects and all custom sObject, then on the basis of the selected sObject I am showing all phone type fields on the page and when user click on lookup button then she gets a lookup containing all values of phone type as shown in screen shots and Video, click for Demo
Please Download complete code from here.
Example Apex Code
Example Apex Code
/* @Auther: Mohammad Usman @Version: 1.0 */ public with sharing class CustomLookupManager { public String selectedPhoneNumber {get;set;} public String selectedPhoneNumberContactName {get;set;} public String selectedObject {get;set;} public String selectedField {get;set;} public list<PhoneNumberDetail> getPhoneLocationsForGivenNumber1 {get;set;} private Map<String, Schema.SObjectType> schemaMap{ get{ if(schemaMap == null){ schemaMap = Schema.getGlobalDescribe(); } return schemaMap; }private set{} } public list<selectOption> allFieldsForSelectedObject {get;set;} /*Controller*/ public CustomLookupManager(){ allFieldsForSelectedObject = new list<selectOption>(); } /*To get all object which have phone numbers*/ public list<selectOption> getsObjects(){ selectedPhoneNumber = ''; //to reset phone numbers field list<selectOption> objectsList = new list<selectOption>(); list<String> tempObject = new list<String>{'Account','Lead', 'Opportunity','Contact','Case'}; objectsList.add(new SelectOption('','--Select Object--')); Map<String, Schema.SObjectType> gd = schemaMap; Schema.DescribeSObjectResult r; Schema.SobjectType sobjecttype; for(String objectKey: gd.keySet()){ sobjecttype = gd.get(objectKey); r = sobjecttype.getDescribe(); if((r.getName()).contains('__c')) objectsList.add(new SelectOption(r.getName(), r.getLabel())); } for(string obj:tempObject){ objectsList.add(new selectOption(obj,obj)); } return objectsList; } /*To get all fields on the basis of object*/ public list<selectOption> getFieldsForSelectedObject(){ selectedPhoneNumber = ''; //to reset hone number field list<selectOption> fieldsName = new list<selectOption>(); try{ if(selectedObject != null || selectedObject != '' || selectedObject != '--Select Object--'){ Map<String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject). getDescribe().fields.getMap(); for(Schema.SObjectField sfield : fieldMap.Values()){ schema.describefieldresult dfield = sfield.getDescribe(); schema.Displaytype disfield= dfield.getType(); system.debug('#######' + dfield ); if(dfield.getType() == Schema.displayType.Phone){ fieldsName.add(new SelectOption(dfield.getName(),dfield.getLabel ())); } } } }catch(Exception ex){ apexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'There is no Phone or Fax Field Exist for selected Object!')); } return fieldsName; } /*to populate field*/ public void populateFields(){ allFieldsForSelectedObject = getFieldsForSelectedObject(); } /*Phone Number's Records*/ public class PhoneNumberDetail{ public String contactName{get;set;} public PhoneNumberDetail(String contactName){ this.contactName = contactName; } } }
Example Visualforce
<!--@Auther: Mohammad Usman @Version: 1.0 --><table class="list" border="0" cellspacing="0" cellpadding="0" style="width:44% !important"> <tr class="headerRow"> <th><b>Select Object</b></th> <th><b>Select Field</b></th> <th><b>Value</b></th> </tr> <tr> <td style="white-space:nowrap;"> <apex:selectList size="1" value="{!selectedObject}" id="selectedObjectId"> <apex:selectOptions value="{!sObjects}" /> <apex:actionSupport event="onchange" action="{!populateFields}" rerender="selectfield,SearchPanelId" status="ajaxStatus"/> </apex:selectList> <apex:actionStatus id="ajaxStatus"> <apex:facet name="start"> <apex:image value="{!$Resource.ajaxLoad}" height="16" width="16" /> </apex:facet> </apex:actionStatus> </td> <td> <apex:selectList size="1" value="{!selectedField}" id="selectfield" > <apex:selectOption itemValue="" itemLabel="--Select--"/> <apex:selectOptions value="{!allFieldsForSelectedObject}"/> </apex:selectList> </td> <td style="white-space:nowrap;"> <apex:inputText id="phoneNumberFieldId" value="{!selectedPhoneNumber}"/> <apex:inputHidden id="phoneNameFieldId" value="{!selectedPhoneNumberContactName}"/> <a href="#" id="acc3_lkwgt" onclick="openLookupPopup();" tabindex="2" title="Parent Account Lookup (New Window)" style="text-decoration: none;"> <img src="/s.gif" alt="Parent Account Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Parent Account Lookup (New Window)"/> </a> <span class="helpButton" id="Phone.Search-_help"> <label for="phoneNumberFieldId"></label> <img src="/s.gif" alt="" class="helpOrb" title=""/> <script type="text/javascript"> sfdcPage.setHelp('Phone.Search', '<table><tr><td><center></center> </td></tr><tr><td>Custom lookup Example</td></tr></table>'); </script> </span> </td> </tr> </table>
0 comments:
Post a Comment