Activity Forums Salesforce® Discussions How to Create a Visualforce page that opens a custom lookup page in Salesforce?

  • shariq

    Member
    September 13, 2018 at 11:24 pm

    Hi,

    Try this code -

    Apex Class :-

    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;
    }
    }
    }

    Visualforce page code:-

    <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">
    &nbsp;
    <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>

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos