Free Advanced Search Code

Written by ShamrockCRM on March 4, 2009 – 9:41 pm

Ok, I have decided to be kind.  Everyone saw the post about the Advanced Search tool for already and loved it.  Everyone requested it and I am in a very giving mood.

This is such a great little tool.  You can’t miss out on this little bit of functionality.

This code is incredibly simple and very easy to install.  This can be used on any edition of Salesforce, including Group, Professional, Enterprise and Unlimited.

1) All you need to do is create a Salesforce Home Page Component.

2) Make this component of type “HTML Area.”

3) Click the checkbox that says “Show HTML.”

4) Add this snippet of HTML into the box:

<script language="JavaScript" type="text/javascript">
    function submitSearchForm(){
        var searchField = document.getElementById('advsearchfield');
        document.advsrch.action = '/search/SearchResults?searchType=2&sen=0&setLast=1&str=' + searchField.value;
        return true;
<form name="advsrch" method="post" onsubmit="return submitSearchForm();"><input class="searchTextBox" id="advsearchfield" maxlength="80" size="18" name="sbstr"> <input class="btn" value=" Go! " type="submit"></form>

5) Add this Home Page Component to your Home Page Layout.

6) Done

Wasn’t that easy?  This will now allow you to have one simple search box, remove the old complicated one and search through all custom fields.  (external identifiers are excellent!)

Let me know if you need help installing this!

***Updated 4/1/2009 – Thanks Mike for the Small Improvement to the Code!
***Updated 4/9/2010 – Glenn provided a nifty way to prevent the search box from stealing cursor focus here. He suggests to show an image of a search box and replacing the image on mouseover with the actual search box. You need to take the image from his link above and use the following code below:

  onmousemove="if(getElementById('textBoxImage')) {document.getElementById('textBoxDiv').innerHTML='<input id=\'searchField\' maxlength=\'80\' name=\'sbstr\' size=\'18\' value=\'\' type=\'text\'><input value=\' Go! \' class=\'btn\' type=\'submit\'>'}">
  <div id="textBoxDiv">
    <img id="textBoxImage" src="/resource/1269872073000/TextBoxImage2">

If you are having problems with the Javascript version, use the below plain HTML version

<form action="/search/SearchResults?searchType=2&sen=0&setLast=1" method="post" name="advsrch"><input class="searchTextBox" maxlength="80" name="sbstr" size="18" value="" type="text"> <input value=" Go! " class="btn" type="submit"></form>

42 Responses to “Free Advanced Search Code”

  1. By Shannon on Mar 23, 2009 | Reply

    I must say thank you for a nice little piece of code. I would buy you a beer.

  2. By Tanner - Shamrock CRM on Mar 23, 2009 | Reply

    I’ll take an AmberBock, please :)

  3. By David- Kent Health Systems on Mar 24, 2009 | Reply

    You rock … Thank you for your efforts and generosity :)

  4. By Jenna on Mar 30, 2009 | Reply

    Thank you so much for sharing this. Might not seem like a big deal, but this made me a big hit with my users. I went from being a system admin, to being the Wizard of Oz.

  5. By Tanner - Shamrock CRM on Mar 30, 2009 | Reply

    Jenna, you win the prize for the best comment EVER!

  6. By Lisa on Mar 31, 2009 | Reply

    You rock Tanner. Thanks for sharing.

  7. By Mike on Apr 1, 2009 | Reply

    Excellent! This is exactly what my org needed. I made a slight change so that the search keyword is displayed in the search box with the results.

  8. By Valentino Rijhen on Apr 2, 2009 | Reply

    Hi Tanner,

    My javascript tag is being removed!

    Any idea?
    Do I have to configure some settings?

    Thanks in advance!

    Valentino Rijhen

  9. By Jenna on Apr 7, 2009 | Reply

    Tanner: And it’s no joke. Seriously. My users now call me Ozzie. Not sure how I feel about that yet, but it could be worse!

  10. By Mark R on Apr 8, 2009 | Reply

    Woot! We have a ton of integration and syncs into custom fields, and this is perfect. Our agents no longer need to pull up their RMA tool nor click into advanced search to pull up RMA or store records, etc. Thanks a bunch!

  11. By Steve Fox on Apr 13, 2009 | Reply

    Tanner – apprecaite the code. It looks like I have the same problem implementing as Velentino. The JavaScript code is being stripped from the HTML Component. Any suggestions?

  12. By Ben on Apr 30, 2009 | Reply

    This is the same as advanced search in salesforce. there’s nothing new here aside from the fact that it’s accessible via the ‘Home’ page. Am I correct?


  13. By Jeff Davidson on May 20, 2009 | Reply

    Tanner – We have implemented this, and many of our users really like it, but….

    When we are using it with the Call Center Connector, it forces the cursor into the Advanced Search box whenever the page or frame refreshes. This can can result in the page scrolling upward when you don’t want it to.

    Is there a way to edit the code so it doesn’t force the cursor into the box?


  14. By Harm Korten on Jun 5, 2009 | Reply

    Another way of doing the same, without adding new components is this.

    Edit the messages and alerts homepage component, add the following 3 lines of code (anywhere in the content of messages and alerts):

    document.getElementById(‘searchType’).value = ‘2’;

    This will actually have the default searchbox, do an advanced search!

  15. By Harm Korten on Jun 5, 2009 | Reply

    Seems I cannot post tags, they got removed from my post….

  16. By Ryan on Jul 3, 2009 | Reply


    Is there any other way i can get that piece of code. It would be really useful

  17. By Glenn Weinstein on Nov 24, 2009 | Reply

    I love the “Advanced Search” component, but also have the same frustration (the cursor deafults into the textbox, causing some jarring scrolling if you’ve already started to page down while the page is loading). Any solutions?

  18. By Eric Taylor on Dec 24, 2009 | Reply

    How do we replace the standard Search box on all pages (not just the Home Page)?

    Thank you.

  19. By Kristin on Dec 31, 2009 | Reply

    This is great, thank you for sharing!

  20. By TJ on Jan 22, 2010 | Reply

    I too would like to know how to add this search component to all objects, instead of the homepage only.

  21. By ShamrockCRM on Jan 22, 2010 | Reply

    Setup->Customize->User Interface->Show Custom Sidebar Components on All Pages.

  22. By TJ on Jan 22, 2010 | Reply

    Awesome. Thank you!

  23. By Marcos. on Feb 3, 2010 | Reply

    Thank you very much! I think this solution is nothing but espectacular!

    It works with excelence… and it’s something many people asked for. In Spain we use the CIF (Fiscal Identification Code) for almost all searches… that’s a custom field in salesforce. This search feature rocks!

    Thank you very much!

  24. By Glenn Weinstein on Apr 3, 2010 | Reply

    I’ve answered my own question here from 24 Nov 09. A colleague and I stumbled upon what we think is a pretty clever technique to avoid having the Advanced Search component always steal focus.

    I’ve posted instructions & code for our technique on the Appirio Tech Blog:

    Please try & enjoy!


  25. By Charlotte on Apr 9, 2010 | Reply

    This doesn’t work – the homepage just shows the text of the html code. What am I doing wrong?

  26. By ShamrockCRM on Apr 9, 2010 | Reply

    Hi Charlotte. Try copying the code into notepad first. Then, create the homepage component and paste the text into it while in HTML mode.

  27. By Charlotte on Apr 9, 2010 | Reply

    Never mind, I figured it out. I had to check the ‘show html’ box before I pasted in the code.

  28. By Malinda on Apr 28, 2010 | Reply

    Hi Tanner – this is a great tool. Thank you!

    I do have a few questions. Is there a way to incorporate the solution search in here as well? (Everything in one box) I still don’t understand why SFDC keeps this seperate from an advanced search.

    Also when doing an advanced search and searching for a contacts last name and the account name, the search doesn’t return what is expected….you have to search for one or the other and then go to the related list to make you choice. Is there a way this can be done as well?


  29. By Sunil K on May 5, 2010 | Reply

    Hi There,
    Its Great. But, I was wondering, if we can modify the code just to display results for the object selected by the user rather than for all Objects

  30. By Daniel Chalef on Jun 30, 2010 | Reply

    As with some other folks commenting, I don’t see results returned for Custom Fields.

    Do I need to turn something else on to make this happen?

  31. By Sunil K on Jul 9, 2010 | Reply

    Its working great but howevee a small problem, the cursor always stays in the search box,but when a user modifies any of the look up fields until the user moves the cursor into the lookup field the cursor stays in the search box and if the user overlooks this and clicks enter (which will now do the search) all the data that he has input is lost causing some inconvenience to the users, is there anyways when a user tries to modify the lookup field, the cursor moves into that field rather than staying in the search box.

  32. By Ann Morgan-Celorier on Nov 30, 2010 | Reply

    WOW – what a quick fix. Totally pleased with this shared code.

  33. By Jonny on Jan 4, 2012 | Reply

    if you’re ever in South Africa , need to buy you a beer for providing this !! Thanks a lot and cheers !! Best wishes for 2012 !

  34. By Vaishnavi on Jan 16, 2012 | Reply

    hi… Thanks for this feature, it helped a lot.
    It would be more helpful, If you can say what each attribute means.
    Like the “searchType”,” sen”, “setLast”.. etc
    I guessed that “search type” means searching for substring or the complete string, but the others could’nt be guessed

    Thanks in Advance

  35. By Jason on Feb 22, 2012 | Reply

    This is amazing. We’ve already used it for a few different purposes.

    The only thing that would make this better is if we could pass additional variables, for example State=NY or Industry=Automotive.

    Any suggestions there?

  36. By Mats on Aug 21, 2012 | Reply

    Thx for this solution. Sweet success! :)


  37. By Ben on Jan 9, 2013 | Reply

    This is still working great, but a couple of notes.
    searchType=2 equals advanced search, 1 equals standard search.
    sen=001 would equal accounts, sen=500 would equal cases. Check your url when you click each object you wish to search for your object id.
    sen=0 equals search all objects.
    I wish I did, but I don’t know what setlast means.

    The above code snippet has the raw html tags, ie. “<” vs. “<".

  38. By Ben on Jan 17, 2013 | Reply

    Is there a way to tack on a “*” to the end of the user’s search string? I’d like to add that to the html version if possible.

  39. By Ben on Jul 18, 2013 | Reply

    Adding search=Search&asPhrase=1 to the url will enable the “exact phrase” function. I’d still like to tack on a * though. :)

  40. By Greg on Aug 9, 2013 | Reply

    Can the code be modified to only search records owned by a specific user or users?

  41. By Nishan on Nov 22, 2013 | Reply

    When I am saving the component, “show HTML” automatically unchecks. So I can’t get this to work. Any suggestions?

  1. 1 Trackback(s)

  2. Dec 30, 2009: » Advanced Search tips & tricks (The, a blog)

Sorry, comments for this entry are closed at this time.