Salesforce: Searching Multiple Objects Using SOQL instead of SOSL

SOQL Across Multiple Objects vs SOSL

Welcome back my children.  Once again, I’ve encountered a situation where I’ve needed to improvise.  For these code examples, remember to use the “expand code (<- ->)” option on the snippets to see everything.

The Issue(s):

1.   I needed a way to be able to take a given object id and determine it’s matching type

2.   I needed to be able to search across multiple objects for matches.

The non- working, but intuitive approach:

I figured I would just do a SOSL search across the types that I know the ID can be matched again.  Then using a basic ternary statement or so, map the type:

 The problem with this approach is that the Id field isn’t indexed in SOSL.  So that leaves me with this option:

The working solution (for under APIv26):

I solved the problem by pushing the results of the SOQL queries into the same kind of list my SOSL would’ve returned any way.   Keep in mind that at least it has a run time of O(C), where C=the number of custom objects to compare against, in my case, C=3:

The Working Solution for APIv26 and up

We can skip the whole ternary statement thing (I still needed the search on multiple objects):

 

There is also the alternative to hard coding the object types to instead say  objResults.size()>0? objResults[0].getSObjectType().getName() : '';  .  The only reason I didn’t use that is because I made reference to run times, and I can no longer account for an actual run time after I start making calls to black box methods such as getSObjectType() and getName().  Quite frankly, the impact should be minimal based on my experience.  Me personally, I use the last solution shown (y).

There you have it!  As usual – stay thirsty. 😉

titancronusSalesforce: Searching Multiple Objects Using SOQL instead of SOSL

Leave a Reply

Your email address will not be published. Required fields are marked *