Friday, 26 April 2013


Most popular requirement is searching in page block table, here I have written a small piece of JavaScript code to search record in page block table i.e. client side fast search, we can also implement search algorithm in apex but that will be server site may take some time to search record it depends on the network connection/traffic.

Here the is a small piece of JavaScript with visual force page, just create a new visual force page and write this code on your page and get easy search implementable in every environment ,developer, enterprises, professional etc. and you can merge this JavaScript code also with apex: repeater ,apex:dataTable, apex:pageBlockTable etc Demo.
Visualforce Page Code:

<!--
    Title: Record search in pageblock 
    Table client side Using javaScript
    Description: This is used to run time fast 
    client side search record in table after entering text in Text box.
   
 -->
<apex:page controller="CountryDisplay" sidebar="false">
    <apex:includeScript value="{!$Resource.jquery171min}" />
    <apex:form >
        <apex:sectionHeader title="Country" subtitle="Phone Code" />
        <apex:pageMessages id="errors" />
        <apex:pageBlock title="Find Country Code !" mode="edit">
            <table width="100%" border="0">
                <tr>
                    <td width="200" valign="top">
                        <apex:pageBlock title="Parameters" mode="edit" id="criteria">
                            <table cellpadding="2" cellspacing="2">
                                <tr>
                                    <td style="font-weight: bold;">
                                    Search By Country Name <br />
                                    <input type="text" id="search" /></td>
                                    <tr>
                                        <td>
                                        <div style="text-align: center; font-size: 18px; 
                                                font-family: Calbiri; color: Black;">
                                         Time(HH:MM:SS)
                                        <div id="clock" style="text-align: center;
                                          font-size: 30px;">
                                        </div>
                                        </div>
                                        </td>
                                    </tr>
                                </tr>
                            </table>
                        </apex:pageBlock>
                    </td>
                        <td valign="top"><apex:pageBlock mode="edit" id="results">
                            <apex:pageBlockTable value="{!tempCountry}" var="contact"
                                columns="3" id="tblData">
                                <apex:column width="300">
                                    <apex:facet name="header">
                                        <apex:commandLink value="Country" action="{!toggleSort}"
                                            rerender="results,debug" />
                                    </apex:facet>
                                    <apex:outputText value="{!contact.PhoneNumber}" />
                                </apex:column>
                                <apex:column width="300">
                                    <apex:facet name="header">
                                        <apex:commandLink value="Idd Country Code"
                                            action="{!toggleSort}" rerender="results,debug" />
                                    </apex:facet>
                                    <apex:outputText value="{!contact.IddCountryCode}" />
                                </apex:column>
                                <apex:column width="200">
                                    <apex:facet name="header">
                                        <apex:commandLink value="Iso3Code" action="{!toggleSort}"
                                            rerender="results,debug" />
                                    </apex:facet>
                                    <apex:outputText value="{!contact.Iso3code}" />
                                </apex:column>
                            </apex:pageBlockTable>
                        </apex:pageBlock>
                    </td>
                </tr>
            </table>
        </apex:pageBlock>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#search').keyup(function(){
                    searchTable($(this).val());
                });
            });
            /*@Usman
                Param 1:searchText
                Description: this method takes an argument and
                search records in the assigned table Id.
            */
            function searchTable(searchText)
            {
                /*get Table using its Id */
                var table = $('table[id$=tblData]');
                table.find('tr').each(function(index, row){
                    var allCells = $(row).find('td');
                    if(allCells.length > 0)
                    {
                        var found = false;
                        allCells.each(function(index, td)
                        {
                            var regExp = new RegExp(searchText, 'i');
                            if(regExp.test($(td).text()))
                            {
                                found = true;
                                return false;
                            }
                        });
                        if(found == true)$(row).show();else $(row).hide();
                    }
                });
            }            
            /*@Usman 
                Set method for every second time interval
                Parm 1(Clock) :Call custom Method to Run current Time.
                Param 2 :1000 millisecond to run watch second by second
            */
            var int = self.setInterval("clock()",1000);
            /*Clock method*/
            function clock(){
                var d=new Date();
                var t=d.toLocaleTimeString();
                document.getElementById("clock").innerHTML = "<B>" + t + "</B>";
            }
        </script>
    </apex:form>
</apex:page>

Controller:
public class CountryDisplay
{
    public String Country { get; set; }
    public String CountryWrapper { get; set; }
    public List<CountryWrapper> tempCountry {get;  set;} 
    public CountryDisplay(){
        tempCountry = new List<CountryWrapper>();
        tempCountry.add(new CountryWrapper('Albania','+355','ALB'));
        tempCountry.add(new CountryWrapper('Algeria','+213','DZA'));
        tempCountry.add(new CountryWrapper('Andorra','+376','AND'));
        tempCountry.add(new CountryWrapper('Angola','+244','AGO'));
        tempCountry.add(new CountryWrapper('Anguilla','+1264','AIA'));
        tempCountry.add(new CountryWrapper('Antigua and Barbuda','+1268','ATG'));  
        tempCountry.add(new CountryWrapper('Argentina','+54','ARG'));
        tempCountry.add(new CountryWrapper('Armenia','+374','ARM'));
        tempCountry.add(new CountryWrapper('Aruba','+297','ABW'));
        tempCountry.add(new CountryWrapper('Ascension Island','+247',''));
        tempCountry.add(new CountryWrapper('Australia','+61','AUS'));
        tempCountry.add(new CountryWrapper('Austria','+43','AUT'));  
        tempCountry.add(new CountryWrapper('Azerbaijan','+994','AZE'));
        tempCountry.add(new CountryWrapper('Bahamas','+1242','BHS'));
        tempCountry.add(new CountryWrapper('Bahrain','+973','BHR'));
        tempCountry.add(new CountryWrapper('Bangladesh','+880','BGD'));
        tempCountry.add(new CountryWrapper('Barbados','+1246','BRB'));
        tempCountry.add(new CountryWrapper('Belarus','+375','BLR'));  
        tempCountry.add(new CountryWrapper('Belgium','+32','BEL'));
        tempCountry.add(new CountryWrapper('Belize','+501','BLZ'));
        tempCountry.add(new CountryWrapper('Benin','+229','BEN'));
        tempCountry.add(new CountryWrapper('Bermuda','+1441','BMU'));
        tempCountry.add(new CountryWrapper('Bhutan','+975','BTN'));
        tempCountry.add(new CountryWrapper('Bolivia','+591','BOL'));  
        tempCountry.add(new CountryWrapper('Bosnia','+387',''));
        tempCountry.add(new CountryWrapper('Botswana','+267','BWA'));
        tempCountry.add(new CountryWrapper('Brazil','+55','BRA'));
        tempCountry.add(new CountryWrapper('British Virgin Islands','+1284',''));
        tempCountry.add(new CountryWrapper('Brunei','+673',''));
        tempCountry.add(new CountryWrapper('Bulgaria','+359','BGR'));  
        tempCountry.add(new CountryWrapper('Burkina Faso','+226','BFA'));
        tempCountry.add(new CountryWrapper('Burundi','+257','BDI'));
        tempCountry.add(new CountryWrapper('Cambodia','+855','KHM'));
        tempCountry.add(new CountryWrapper('Cameroon','+237','CMR'));
        tempCountry.add(new CountryWrapper('Canada','+1','CAN'));
        tempCountry.add(new CountryWrapper('Cape Verde Islands','+238',''));  
        tempCountry.add(new CountryWrapper('Cayman Islands','+1345','CYM'));
        tempCountry.add(new CountryWrapper('Central Africa Republic','+236',''));
        tempCountry.add(new CountryWrapper('Chad','+235','TCD'));
        tempCountry.add(new CountryWrapper('Chile','+56','CHL'));
        tempCountry.add(new CountryWrapper('China','+86','CHN'));
        tempCountry.add(new CountryWrapper('Columbia','+57',''));  
        tempCountry.add(new CountryWrapper('Comoros Island','+269',''));
        tempCountry.add(new CountryWrapper('Congo','+242','COG'));
        tempCountry.add(new CountryWrapper('Cook Islands','+682','COK'));
        tempCountry.add(new CountryWrapper('Costa Rica','+506','CRI'));
        tempCountry.add(new CountryWrapper('Croatia','+385','HRV'));
        tempCountry.add(new CountryWrapper('Cuba','+53','CUB'));  
        tempCountry.add(new CountryWrapper('Cyprus','+357','CYP'));
        tempCountry.add(new CountryWrapper('Czech Republic','+420','CZE'));
        tempCountry.add(new CountryWrapper('Democratic Republic of Congo (Zaire)','+243',''));
        tempCountry.add(new CountryWrapper('Denmark','+45','DNK'));
        tempCountry.add(new CountryWrapper('Diego Garcia','+246',''));
        tempCountry.add(new CountryWrapper('Djibouti','+253','DJI'));  
        tempCountry.add(new CountryWrapper('Dominica Islands','+1767',' '));
        tempCountry.add(new CountryWrapper('Dominican Republic','+1829',' '));
        tempCountry.add(new CountryWrapper('Dominican Republic','+1849',' '));
        tempCountry.add(new CountryWrapper('Dominican Republic','+1809','DOM'));
        tempCountry.add(new CountryWrapper('Ecuador','+593','ECU'));
        tempCountry.add(new CountryWrapper('Egypt','+20','EGY'));  
        tempCountry.add(new CountryWrapper('El Salvador','+503','SLV'));
        tempCountry.add(new CountryWrapper('Equatorial Guinea','+240','GNQ'));
        tempCountry.add(new CountryWrapper('Eritrea','+291','ERI'));
        tempCountry.add(new CountryWrapper('Estonia','+372','EST'));
        tempCountry.add(new CountryWrapper('Ethiopia','+251','ETH'));
        tempCountry.add(new CountryWrapper('Faeroe Islands','+298',''));
        tempCountry.add(new CountryWrapper('Falkland Islands','+500',' '));
        tempCountry.add(new CountryWrapper('Fiji Islands','+679',' '));
        tempCountry.add(new CountryWrapper('Finland','+358','FIN'));
        tempCountry.add(new CountryWrapper('France','+33','FRA'));
        tempCountry.add(new CountryWrapper('French Guiana','+594','GUF'));
        tempCountry.add(new CountryWrapper('French Polynesia','+689','PYF'));
        tempCountry.add(new CountryWrapper('Gabon','+241','GAB'));
        tempCountry.add(new CountryWrapper('Georgia','+995','GEO'));
        tempCountry.add(new CountryWrapper('Germany','+49','DEU'));
        tempCountry.add(new CountryWrapper('Ghana','+233','GHA'));
        tempCountry.add(new CountryWrapper('Gibraltar','+350','GIB'));
        tempCountry.add(new CountryWrapper('Greece','+30','GRC'));  
        tempCountry.add(new CountryWrapper('Greenland','+299','GRL'));
        tempCountry.add(new CountryWrapper('Grenada','+1473','GRD'));
        tempCountry.add(new CountryWrapper('Guadeloupe','+590','GLP'));
        tempCountry.add(new CountryWrapper('Guam','+1671','GUM'));
        tempCountry.add(new CountryWrapper('Guatemala','+502','GTM'));
        tempCountry.add(new CountryWrapper('Guinea Bissau','+245',''));
        tempCountry.add(new CountryWrapper('Guinea Republic','+224',' '));
        tempCountry.add(new CountryWrapper('Guyana','+592','GUY'));
        tempCountry.add(new CountryWrapper('Haiti','+509','HTI'));
        tempCountry.add(new CountryWrapper('Honduras','+504','HND'));
        tempCountry.add(new CountryWrapper('Hong Kong','+852','HKG'));
        tempCountry.add(new CountryWrapper('Hungary','+36','HUN'));
        tempCountry.add(new CountryWrapper('Iceland','+354','ISL'));
        tempCountry.add(new CountryWrapper('India','+91','IND'));
        tempCountry.add(new CountryWrapper('Indonesia','+62','IDN'));
        tempCountry.add(new CountryWrapper('Iran','+98',' '));
        tempCountry.add(new CountryWrapper('Iraq','+964','IRQ'));
        tempCountry.add(new CountryWrapper('Ireland','+353','IRL'));
        tempCountry.add(new CountryWrapper('Israel','+972','ISR'));
        tempCountry.add(new CountryWrapper('Italy','+39','ITA'));
        tempCountry.add(new CountryWrapper('Ivory Coast','+225',' '));
        tempCountry.add(new CountryWrapper('Jamaica','+1876','JAM'));
        tempCountry.add(new CountryWrapper('Japan','+81','JPN'));
        tempCountry.add(new CountryWrapper('Jordan','+962','JOR'));
        tempCountry.add(new CountryWrapper('Kazakhstan','+7','KAZ'));
        tempCountry.add(new CountryWrapper('Kenya','+254','KEN'));
        tempCountry.add(new CountryWrapper('Kiribati','+686','KIR'));
        tempCountry.add(new CountryWrapper('Kuwait','+965','KWT'));
        tempCountry.add(new CountryWrapper('Kyrgyzstan','+996','KGZ'));
        tempCountry.add(new CountryWrapper('Laos','+856',' '));
        tempCountry.add(new CountryWrapper('latvia','+371','LVA'));
        tempCountry.add(new CountryWrapper('Lebanon','+961','LBN'));
        tempCountry.add(new CountryWrapper('Lesotho','+266','LSO'));
        tempCountry.add(new CountryWrapper('Liberia','+231','LBR'));  
        tempCountry.add(new CountryWrapper('Libya','+218',' ')); 
        tempCountry.add(new CountryWrapper('Liechtenstein','+423','LIE')); 
        tempCountry.add(new CountryWrapper('Lithuania','+370','LTU')); 
        tempCountry.add(new CountryWrapper('Luxembourg','+352','LUX')); 
        tempCountry.add(new CountryWrapper('Macau','+853',' ')); 
        tempCountry.add(new CountryWrapper('Macedonia (Fyrom)','+389',' '));
        tempCountry.add(new CountryWrapper('Madagascar','+261','MDG'));
        tempCountry.add(new CountryWrapper('Malawi','+265','MWI'));
        tempCountry.add(new CountryWrapper('Malaysia','+60','MYS'));
        tempCountry.add(new CountryWrapper('Maldives Republic','+960','     '));
        tempCountry.add(new CountryWrapper('Mali','+223','MLI'));
        tempCountry.add(new CountryWrapper('Malta','+356','MLT')); 
        tempCountry.add(new CountryWrapper('Mariana Islands','+1670',' ')); 
        tempCountry.add(new CountryWrapper('Marshall Islands','+692','MHL')); 
        tempCountry.add(new CountryWrapper('Martinique','+596','MTQ')); 
        tempCountry.add(new CountryWrapper('Mauritius','+230','MUS')); 
        tempCountry.add(new CountryWrapper('Mayotte Islands','+262',' '));
        tempCountry.add(new CountryWrapper('Mexico','+52','MEX'));
        tempCountry.add(new CountryWrapper('Micronesia','+691',' '));
        tempCountry.add(new CountryWrapper('Moldova','+373','MDA'));
        tempCountry.add(new CountryWrapper('Monaco','+377','MCO'));
        tempCountry.add(new CountryWrapper('Mongolia','+976','MNG'));  
        tempCountry.add(new CountryWrapper('Montserrat','+1664','MSR')); 
        tempCountry.add(new CountryWrapper('Morocco','+212','MAR')); 
        tempCountry.add(new CountryWrapper('Mozambique','+258','MOZ'));
        tempCountry.add(new CountryWrapper('Myanmar (Burma)','+95','')); 
        tempCountry.add(new CountryWrapper('Namibia','+264','NAM')); 
        tempCountry.add(new CountryWrapper('Nauru','+674','NRU'));
        tempCountry.add(new CountryWrapper('Nepal','+977','NPL'));
        tempCountry.add(new CountryWrapper('Netherlands','+31','NLD'));
        tempCountry.add(new CountryWrapper('Netherlands Antilles','+599','ANT'));
        tempCountry.add(new CountryWrapper('New Caledonia','+687','NCL'));
        tempCountry.add(new CountryWrapper('New Zealand','+64','NZL'));  
        tempCountry.add(new CountryWrapper('Nicaragua','+505','NIC')); 
        tempCountry.add(new CountryWrapper('Niger','+227','NER')); 
        tempCountry.add(new CountryWrapper('Nigeria','+234','NGA')); 
        tempCountry.add(new CountryWrapper('Niue Island','+683',' ')); 
        tempCountry.add(new CountryWrapper('Norfolk Island','+672','NFK')); 
        tempCountry.add(new CountryWrapper('North Korea','+850',' '));
        tempCountry.add(new CountryWrapper('Norway','+47','NOR'));
        tempCountry.add(new CountryWrapper('Oman','+968','OMN'));
        tempCountry.add(new CountryWrapper('Pakistan','+92','PAK'));
        tempCountry.add(new CountryWrapper('Palau','+680','PLW'));
        tempCountry.add(new CountryWrapper('Palestine','+970',' '));  
        tempCountry.add(new CountryWrapper('Panama','+507','PAN')); 
        tempCountry.add(new CountryWrapper('Papua New Guinea','+675','PNG')); 
        tempCountry.add(new CountryWrapper('Paraguay','+595','PRY')); 
        tempCountry.add(new CountryWrapper('Peru','+51','PER')); 
        tempCountry.add(new CountryWrapper('Philippines','+63','PHL')); 
        tempCountry.add(new CountryWrapper('Poland','+48','POL'));
        tempCountry.add(new CountryWrapper('Portugal','+351','PRT'));
        tempCountry.add(new CountryWrapper('Puerto Rico','+1939',' '));
        tempCountry.add(new CountryWrapper('Puerto Rico','+1787','PRI'));
        tempCountry.add(new CountryWrapper('Qatar','+974','QAT'));
        tempCountry.add(new CountryWrapper('Reunion Island','+262',' '));  
        tempCountry.add(new CountryWrapper('Romania','+40','ROU')); 
        tempCountry.add(new CountryWrapper('Russia','+7',' ')); 
        tempCountry.add(new CountryWrapper('Rwanda','+250','RWA'));
        tempCountry.add(new CountryWrapper('Saint Vincent and the Grenadines','+1784',' '));
        tempCountry.add(new CountryWrapper('Samoa (American)','+1684',' ')); 
        tempCountry.add(new CountryWrapper('Samoa (Western)','+685',' '));
        tempCountry.add(new CountryWrapper('San Marino','+378','SMR'));
        tempCountry.add(new CountryWrapper('Sao Tome & Principe','+239',' '));
        tempCountry.add(new CountryWrapper('Saudi Arabia','+966','SAU'));
        tempCountry.add(new CountryWrapper('Senegal','+221','SEN'));
        tempCountry.add(new CountryWrapper('Serbia','+381',' '));  
        tempCountry.add(new CountryWrapper('Seychelles','+248','SYC')); 
        tempCountry.add(new CountryWrapper('Sierra Leone','+232','SLE')); 
        tempCountry.add(new CountryWrapper('Singapore','+65','SGP'));
        tempCountry.add(new CountryWrapper('Sint Maarten','+1721',' ')); 
        tempCountry.add(new CountryWrapper('Slovak Republic','+421',' ')); 
        tempCountry.add(new CountryWrapper('Slovenia','+386','SVN'));
        tempCountry.add(new CountryWrapper('Solomon Islands','+677','SLB'));
        tempCountry.add(new CountryWrapper('Somalia','+252','SOM'));
        tempCountry.add(new CountryWrapper('South Africa','+27','ZAF'));
        tempCountry.add(new CountryWrapper('South Korea','+82',' '));
        tempCountry.add(new CountryWrapper('Spain','+34','ESP'));  
        tempCountry.add(new CountryWrapper('Sri Lanka','+94','LKA')); 
        tempCountry.add(new CountryWrapper('St Helena','+290','     ')); 
        tempCountry.add(new CountryWrapper('St Kitts & Nevia','+1869',' ')); 
        tempCountry.add(new CountryWrapper('St Lucia','+1758',' ')); 
        tempCountry.add(new CountryWrapper('Sudan','+249','SDN')); 
        tempCountry.add(new CountryWrapper('Surinam','+597',' '));
        tempCountry.add(new CountryWrapper('Swaziland','+268','SWZ'));
        tempCountry.add(new CountryWrapper('Sweden','+46','SWE'));
        tempCountry.add(new CountryWrapper('Switzerland','+41','CHE'));
        tempCountry.add(new CountryWrapper('Syria','+963',' '));
        tempCountry.add(new CountryWrapper('Taiwan','+886',' '));  
        tempCountry.add(new CountryWrapper('Tajikistan','+992','TJK')); 
        tempCountry.add(new CountryWrapper('Tanzania','+255',' ')); 
        tempCountry.add(new CountryWrapper('Thailand','+66','THA'));
        tempCountry.add(new CountryWrapper('The Gambia','+220',' ')); 
        tempCountry.add(new CountryWrapper('Togo','+228','TGO')); 
        tempCountry.add(new CountryWrapper('Tonga','+676','TON'));
        tempCountry.add(new CountryWrapper('Trinidad & Tobago','+1868',' '));
        tempCountry.add(new CountryWrapper('Tunisia','+216','TUN'));
        tempCountry.add(new CountryWrapper('Turkey','+90','TUR'));
        tempCountry.add(new CountryWrapper('Turkmenistan','+993','TKM'));
        tempCountry.add(new CountryWrapper('Turks & Caicos Islands','+1649',' '));  
        tempCountry.add(new CountryWrapper('Tuvalu','+688','TUV ')); 
        tempCountry.add(new CountryWrapper('U.S. Virgin Islands','+1340',' ')); 
        tempCountry.add(new CountryWrapper('Uganda','+256','UGA')); 
        tempCountry.add(new CountryWrapper('Ukraine','+380','UKR')); 
        tempCountry.add(new CountryWrapper('United Arab Emirates','+971','ARE')); 
        tempCountry.add(new CountryWrapper('United Kingdom','+44','GBR'));
        tempCountry.add(new CountryWrapper('United States','+1','USA'));
        tempCountry.add(new CountryWrapper('Uruguay','+598','URY'));  
        tempCountry.add(new CountryWrapper('Uzbekistan','+998','UZB')); 
        tempCountry.add(new CountryWrapper('Vanuatu','+678','VUT')); 
        tempCountry.add(new CountryWrapper('Venezuela','+58','VEN')); 
        tempCountry.add(new CountryWrapper('Vietnam','+84',' ')); 
        tempCountry.add(new CountryWrapper('Wallis & Futuna Islands','+681',' '));
        tempCountry.add(new CountryWrapper('Yemen Arab Republic','+967',' '));
        tempCountry.add(new CountryWrapper('Zambia','+260','ZMB'));
        tempCountry.add(new CountryWrapper('Zimbabwe','+263','ZWE'));  
   }
   class CountryWrapper{
      Public string PhoneNumber{get;set;}
      Public string IddCountryCode{get;set;}
      Public string Iso3code{get;set;}
          public CountryWrapper(String PhoneNumber,String IddCountryCode ,String Iso3code){
               this.PhoneNumber=PhoneNumber;
               this.IddCountryCode=IddCountryCode;
               this.Iso3code=Iso3code;
          }
   }
}


Creating (Converting contact into user) customer portal user is basic requirement for writing test cases (may be a requirement) in Salesforce as we know customer portal users contains different profile like the customer portal user's custom profile so first of all assign customer portal profile to user and then create customer portal user, we can use customer portal user in other requirement as well so here we have an example (Apex code script) that will convert contact into customer portal user.

Apex Script
//Retrive a contact from database 
Contact objectContact = [SELECT FirstName,LastName,Email 
                         FROM Contact Where Email <> null limit 1];
//To get converted contact into user
User objContactUser = getConvertedUserFormContact(objectContact);
System.debug('Contact User Deatils:'+objContactUser);
/*  
    @Param: Contact Record
    @description: Pass contact as an argument that 
    contact will be convert into customer portal user.
*/
public User getConvertedUserFormContact(Contact objContact){    
    try{
        if(objContact != null){
            //Select Exist Customer portel User's profile
            Profile pf = [SELECT Id FROM profile 
                          WHERE name='Custom Portal User Profile' limit 1];   
            //Create user 
            User mockUser = new User(contactId=objectContact.Id, 
                            username=objectContact.Email, 
                            firstname=objectContact.FirstName,
                            lastname=objectContact.LastName, 
                            email=objectContact.Email,
                            communityNickname = objectContact.LastName + '_'+Math.random(),
                            alias = string.valueof(objectContact.FirstName.substring(0,1) + 
                                    objectContact.LastName.substring(0,1) + Math.random() ), 
                            profileid = pf.Id, emailencodingkey='UTF-8',
                            languagelocalekey='en_US', 
                            localesidkey='en_US', 
                            timezonesidkey='America/Los_Angeles');                  
            insert mockUser;
        return mockUser;
    }catch(Exception ex){
        throw ex;
    }
    return null;
}


Salesforce Custom Lookup Using Apex



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 Videoclick for Demo


Please Download complete code from here.
Example Apex Code

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

VF Page:

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

    Links