Question

I have a problem with an SOSL statement.

This statement works correctly and returns never more than 2 rows.

List<List<SObject>> searchResultList = null;
searchResultList = [FIND :value IN PHONE FIELDS RETURNING Contact(id, Name, Phone, HomePhone, MobilePhone, OtherPhone, AssistantPhone), Account(id, Name, Phone), User(id, Name, Phone, MobilePhone, Extension) LIMIT 2];

When using a variable in LIMIT it returns more than 2 rows.

List<List<SObject>> searchResultList = null;
Integer maxsuggestions = 2;

searchResultList = [FIND :value IN PHONE FIELDS RETURNING Contact(id, Name, Phone, HomePhone, MobilePhone, OtherPhone, AssistantPhone), Account(id, Name, Phone), User(id, Name, Phone, MobilePhone, Extension) LIMIT :maxsuggestions];

I have no idea what i am doing wrong.

Thank you for helping :)

Greets Johny

Was it helpful?

Solution

YOu need to put the LIMIT in each set of fields. For example take your code above and rewrite it like this:

List<List<sObject>> testval=null;
integer maxsuggestions=2;
string searchvalue='610';
testval=[FIND :searchvalue IN PHONE FIELDS RETURNING Contact(id, Name, Phone, HomePhone, MobilePhone, OtherPhone,    AssistantPhone LIMIT :maxsuggestions), Account(id, Name, Phone LIMIT :maxsuggestions), User(id, Name, Phone, MobilePhone, Extension LIMIT :maxsuggestions)];

Keep in mind that this will return 2 of each object you have specified in the search string so you will get two Users, two Accounts and two Contacts. If you place a LIMIT outside the object field listings, from my testing you need to supply a value as for some reason variables don't seem to work to limit the entire search query. I tested yours with limits of 2, 5 and 10 and they worked as expected but when I tried to use I or a new variable in that query limit position (not in the code sample) it does not work. Below is an example of limiting the entire result set to 3 items.

List<List<sObject>> testval=null;
integer maxsuggestions=2;
string searchvalue='610';
testval=[FIND :searchvalue IN PHONE FIELDS RETURNING Contact(id, Name, Phone, HomePhone, MobilePhone, OtherPhone,    AssistantPhone LIMIT :maxsuggestions), Account(id, Name, Phone LIMIT :maxsuggestions), User(id, Name, Phone, MobilePhone, Extension LIMIT :maxsuggestions) LIMIT 3];

The way this works is if it finds 1 contact and 1 account it could potientially return 1 user record too, however if it is limited to three and it returns 2 contacts, it will return 1 account that matches and stops.

Let me know if you have any other questions or how this works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top