function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

SOSL search on objects all fields?



I have a question regarding SOSL search in context of object fields. Can SOSL be used search against any fields in object(s) or any limitations to it?






String fields ='';

for( String fieldName : Schema.SObjectType.User.fields.getMap().keySet() ) {
	if(fields != '') fields += ', ';
	fields += fieldName;

List<User> users = Database.query('Select ' + fields + ' From User');


You can do this if you wanna get all the fields of an object without listing it manually.




I think, may be I didn't explain my question clearly. For example let say I have an Object called MyProducts, with fields, MyProductName, MyProductFamily, MyProdcutDescription, MyProductCode,


Can I able to do a query like,


FiIND 'shoe' IN MyProductName RETURNING MyProdcuts(MyProdctCode, MyProductFamily)


We can do this it in SOQL, but the point it to konw, upto what extent I can make use of SOSL.





You don't specify the exact fields to search when using SOSL queries - you can restrict the search to particular groups of fields (e.g. all fields, all phone fields, all name fields).  You can specify the fields that you want returned.


There's more detail on the SOSL syntax here:|SkinName=webhelp


hello there, I just would like to confirm if we could also search from other custom objects not just limited to Accounts,Contacts,Opportunities and Leads as per from examples.



Leandro Santillan 6Leandro Santillan 6
@ag_force yes you can. an easy example: " FIND {test} IN ALL FIELDS RETURNING Case " that will search for the word 'test' in the case object.