Appirio's Tech Blog

Monday, March 29, 2010

Advanced Search as a Home Page Component

Glenn Weinstein

Like many SFDC users, we wanted to reduce the clicks needed to run an Advanced Search.  ShamrockCRM posted a clever trick for adding Advanced Search as a custom Home Page Component, which has saved us lots of clicks over the past year.

However, one frustration with the solution is that SFDC waits until a given page finishes loading, and then puts the cursor into your new Advanced Search textbox.  This is annoying if you’ve already started scrolling through, say, a lengthy detail page, only to have your browser jarringly yank you back to the Advanced Search box.

After thinking hard about a clean solution to this – we’re in tricky territory, after all, with SFDC’s undocumented and sometimes hard-to-divine behavior governing its left sidebar – Chris Bruzzi and I stumbled upon a pretty clever new trick. 

The general principle was that we needed to prevent the textbox element from getting focus until the user really wanted to do a search.The first “aha moment” was the idea of setting the textbox to read-only, until the user moused over it.  Good idea, but read-only textboxes can (and will) get focus.  How about disabling the textbox?  This is a better solution since a disabled control can’t get focus.  But, it also can’t respond to mouseover events!  So we settled on putting a 1-pixel-high invisible image above and below the textbox, and tying the mouseover to these images.  Good, but not great – users could still manage to get their pointers into the textbox without touching the images.

Then Chris had the second “aha moment.”  What if we actually showed just an image of the search box, and then when the user moused over it, we replaced the image with the real search box?  Brilliant!  This works perfectly, and seamlessly.

The modified code looks like this:

<form

  id="searchForm"

  action="/search/SearchResults?searchType=2&amp;sen=0&amp;setLast=1"

  method="post"

  name="advsrch"

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

  </div>

</form>

Note that you’ll need to upload an image file of the search box as a Static Resource, and then replace the URL above (/resource/1269872073000/TextBoxImage2) with a link to your Static Resource.  Here’s an image you can use, which is based on the new Spring ‘10 UI:

TextBoxImage2

We added the “Go” button to the image because we found that if the image only contains the textbox, its alignment compared to the Go button was shifting slightly during the image-to-textbox replacement.

We are now happy users of an Advanced Search component that doesn’t steal focus until it’s time!

12 comments:

  1. Wow... Wow! You guys are my heroes. Thank you - both from me (who removed the search component because of the issue with focus stealing) and from my users (for allowing me to add it back in).
    ReplyDelete
  2. Can you post an explanation of how to splice this code into the previous code? For us HTML-newbies, that is?
    ReplyDelete
  3. Hi Anonymous - this code actually replaces the previous code.
    ReplyDelete
  4. Wow. This is *such* a timesaver. Thanks for putting this out there, you guys!
    ReplyDelete
  5. I have used this in a component for a customer portal, but the search results are not being displayed. Kindly help me.
    ReplyDelete
  6. Rillu - I'd be happy to try and help. Email me at glenn (at) appirio.com with further details. Were you able to get it to work in regular (non-portal) SFDC?
    ReplyDelete
  7. Hi Glenn, thanks for this even more handy now that we're testing chatter and not happy with the new search functionality.

    However, the one object that is not represented is Opportunities, i can search for an opp by name and get the result, but if i search for an account and there is an opp on the account i'm not getting the opp back on the results only the account reluts, might you know why?

    Thanks,

    Jimmy
    ReplyDelete
  8. Hi Jimmy - I think that's just how advanced search works in SFDC. According to the SFDC help file (https://help.salesforce.com/apex/HTViewHelpDoc?id=search_fields.htm&language=en#Opportunities), advanced search only searches 2 fields on Opportunity records: name and description. So if you search an a company name, you'll only get opportunities back as results if the company name appears in the opportunity name or description.
    ReplyDelete
  9. Thank you Glenn, I just did some more testing with your code and results and the standard search in our Prod enviornment and get the same results.

    FYI, what was throwing me off was I knew that in my prod org i recalled seeing opps in the search results when I searched on a company name. It's because a related list on the opp contains the account name, the related list is "partners" but not all our accounts have this populated.

    I did not use the same comany in testing and the firms i was using did not have the Partners related list populated.

    Again, thanks for the code and the response, greatly appreciated by this non developer.

    Jimmy
    ReplyDelete
  10. It's not working the cursor still goes back to the search box.

    Regards,
    Praveen Jaswal
    ReplyDelete
  11. Hi Praveen - that's not possible, if you follow my suggestion in this blog. If you're not actually showing a search box, but rather an image of a search box, there's no way a fake search box is going to get the cursor! You should probably re-read this blog. If you still have issues, please contact me directly (glenn at appirio.com).
    ReplyDelete

 
2006-2012 Appirio Inc. All rights reserved.
Appirio.com | Support | Resource Center | Contact | Careers | Privacy Policy