Sunday, October 7, 2012


Standard lookup - override with visual Force :

Sometimes we need a visual force page instead of standard lookup to select the proper record based on few filters if  standard lookup filters do not support. The only option we had to use this custom lookup page, to override the standard edit page. 

Managing a custom edit page, really a pain for Case or Opportunity because we have to take care of all other standard functionality, need controller, test classes additional to custom lookup page.

Personally I found that this is a very common and complex requirement, that is why sharing this to all to save your time and efforts.

There are two ways to override the standard lookup without overriding the standard edit page.

1) Page Layout section header
2) Home page component

Lets start with opportunity account lookup:

1) Page Layout section header

  • First create a JS file with this code:


function addEventOnLoad(func) {
                                var oldOnLoad = window.onload;
                                if (typeof window.onload != 'function') {
                                                if (oldOnLoad) {
                                                                window.onload = function() {
                                                                                eval(oldOnLoad); func();
                                                                }
                                                } else {
                                                                window.onload = func;
                                                }
                                } else {
                                                window.onload = function() {
                                                                if (oldOnLoad) oldOnLoad(); func();
                                                }
                                }
                }
                function openLookupWindow(baseURL,width,modified,searchParam) {
                                if (modified == '1')
                                                baseURL = baseURL + searchParam;
                                openPopup(baseURL, "lookup", 350, 480, "width="+width+",height=575,toolbar=no,status=no,directories=no,menubar=no, resizable=yes, scrollbars=yes", true);
                }
                function overrideAccountLookup() {
                                var associatedLink = document.getElementById('opp4_lkwgt');
                                if (associatedLink != null) {
                                                associatedLink.href = "JavaScript: openLookupWindow('/apex/VFPAGENAME?isOpp=true&lkfm=editPage&lknm=opp4&lktp=' + getElementByIdCS('opp4_lktp').value,680,getElementByIdCS('opp4_mod').value,'&lksrch=' + escapeUTF(getElementByIdCS('opp4').value))";
                                }
                }
                addEventOnLoad(overrideAccountLookup);



  • Replace /apex/VFPAGENAME with your visual force page name.  opp4_lkid is the Id property of account lookup field. you can replace this value with other lookup fields Id.  Please replace only "opp4" leave all other characters such as _lkid or _lktp.



  • Save this file as a static resource or any document folder and use the link to create a script statement like: 

<script src="/servlet/servlet.FileDownload?file=01530000000pkp"></script>



  • Edit your Opportunity Page Layout section header



Note: Maximum length of Section Name is 80 char so you can create a new blank section.

2) Using home page component:


  •    Create a new home page component type HTML area.
  •    Click on show HTML and paste the same JS file code.
  •    You can give component name in such a way so that it will look like invisible. for ex: few dots "...." or a small line "___".
  •    Add this component to your home page layouts.



In this way you can save your time and efforts and no need to create custom edit page to just override a lookup.

Waiting for your valuable suggestion.....



4 comments:

  1. Its not working, could you please help me

    ReplyDelete
    Replies
    1. how can i contact you..? Please send me your detail on pandeypankaj20@gmail.com

      Delete
    2. Hey I sent an Email could you please confirm from in gmail

      Delete
    3. Could you please call me? Its urgent for me?? phani.cse29@gmail.com my mail Id

      Delete