Hot answers tagged

58

Let's make a quick comparison of the two: Definition SOQL (Salesforce Object Query Language) Use the Salesforce Object Query Language (SOQL) to construct simple but powerful query strings in the queryString parameter in the query() call, in Apex statements, in Visualforce controllers and getter methods, or in the Schema Explorer of the Force.com ...


23

SOSL has the ability to search across multiple objects in one query. So if you do something like the following it will return results from Accounts, Contacts, Opportunities, and Leads. List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; Account [] accounts = ((List<Account>)...


17

If you use : in your query, Apex expects a variable. In case you are querying with the string, you should not use the :, but escape single quotes(\'). So your query should look like this: asset = Database.query('SELECT ID, Price, Asset_MRR__c FROM Asset WHERE id =\'' + asset.id + '\' limit 1'); You could also use :, but then just put asset.id in your query ...


14

I haven't seen documentation other than what you linked to and the documentation of Test.setFixedSearchResults(). I understand the distinction you are making. I have written tests on classes that contain methods that use SOSL. As part of pre test set up, in the test method, I have used the fixedSearchResults. I have never had any issues. Something like ...


13

You can not use the LIKE operator with an Id field. As per the documentation: LIKE Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes ...


12

You didn't mention it was an unit test context :) Testing SOSL is somewhat messy because of indexing delay. You might have to do manual tests of your functionality first and get back to the unit test once you're happy with it. Then in actual code play with Test.setFixedSearchResults(). Check http://www.salesforce.com/us/developer/docs/apexcode/Content/...


11

I think you should update your query to FIND 'Cisco Systems*' IN ALL FIELDS RETURNING Account( ownerid,name,website WHERE (name LIKE '%Cisco Systems%' ) OR (Website_Domain__c LIKE '%cisco.com%' ) OR (phone LIKE '%4085253777%' ) ORDER BY Oppty_Count__c LIMIT 999 ) LIMIT 2000 Just moved the Oppty_Count__c inside the braces. For multiple object types,...


10

The reason why my code was not working - and this really makes no sense to me - is a missing package namespace: I replaced this line: List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ALL FIELDS RETURNING SObjectWork__c(Id, ltxt_SObjectIds__c)]; with this one List<List<SObjectWork__c>> soslResults = [FIND :orPhrase IN ...


10

You can perform a normal SOQL query. The reason why you can't wildcard at the beginning is because the index, like in all database systems, indexes from the beginning of each word. In the case where you have a word like "Home", the SOSL index would contain an entry like: Term Matching Ids Home Record1, Record5, Record27, ... Thus, it can match Ho*...


10

It is incorrect to assume that SOSL is better for large data sets. Both SOQL and SOSL can be used with large data sets when implemented correctly. I wrote an article (originally posted as a Q&A here) about the differences between SOQL and SOSL you may find useful. SOSL actually limits you to 200 records returned in a single SOSL query while SOQL will ...


9

Solution 1: Use @RemoteAction instead of standard rendering. You'll need JavaScript. Example: Controller: public with sharing class searchall { @RemoteAction public static string[] searchableElements() { String[] terms = new String[0]; for(Sobjecttype t:schema.getglobaldescribe().values()) if(t.getdescribe().issearchable())...


9

Not sure why that part of the documentation is not mentioning - and + characters, but they are reserved according to this documentation: Reserved Characters The following characters are reserved: ? & | ! { } [ ] ( ) ^ ~ * : \ " ' + - Reserved characters, if specified in a text search, must be escaped (preceded by the backslash \ character)...


9

For all intents and purposes, the default order is indeed the same for both SOSL and Global Search, as they use the same search indexes and algorithms. This places the API and Global Search on equal footing; a developer can rely on their own app (e.g. a mobile app) returning the same types of results that would be expected in the UI. Search ranking is ...


8

According to the SOQL Documentation on Comparison Operators the Like is just for Strings. Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. You could create a Text formula field and give it the value of the Id field by specifying Id in the formula editor. Then your query would ...


8

SOSL does not support traversing relationship query . When you write a query as below List<List<SObject>> searchList = [FIND :mySearchText IN ALL FIELDS RETURNING Account (id, name,phone, BillingStreet,BillingCity,BillingState, owner.Alias,owner.MobilePhone )]; You will notice that owner....


8

As the "mark dirty" process, can you consider submitting a Batch Job using a Query Locator? It permits up to 50,000,000 records. public class DirtyJob implements Database.Batchable<sObject> { public Database.QueryLocator start(Database.BatchableContext context) { return Database.getQueryLocator('SELECT Id FROM Filthy__c'); } ...


8

SOSL is indexed asynchronously. You can't rely on the results being available in real-time, or even within a few seconds after the final commit. Use SOQL if you need potentially real-time results.


8

As per the Developer Guide on SOSL WHERE clause, it is used to filter the search results: By default, a SOSL query on an object retrieves all rows that are visible to the user. To limit the search, you can filter the search result by specific field values. Furthermore, the syntax of the SOSL also suggests that the WHERE clause is applied to search ...


7

SOSL is basically running queries against Solr, so you may want to look for info on Solr data-indexing times (and account for some Salesforce-related possible overhead). Proof that Salesforce.com uses Solr, in case you didn't totally believe it. :)


7

While it looks to be undocumented, formula fields do not appear to be supported by SOSL. The search() call searches most objects (including custom objects) and text fields to which you have access. It does not search the following objects and fields: Any elements such as picklists that are defined as not searchable (searchable is false). To ...


7

Found a way to do this in SOSL but with major caveats: the clauses in RETURNING are applied after the search, so if the result limits are reached, the records won't be found. (courtesy superfell) Trick is to satisfy the FIND clause with '* OR *' and use the WHERE clause to do the selecting: [ FIND '* OR *' RETURNING Account (Name WHERE Id = '...


7

If you want get the parent object information then you need to use like this. SOQL Child to Parent for(Contact objContact: [SELECT Id, LastName, AccountId, Account.Name FROM Contact]) { system.debug('====Account Name======'+objContact.Account.Name); } if you want any other info about Account then you need to use Account.FieldAPIName... this way you will ...


7

The why is lost to time, I doubt there was a conscience decision. [] is a bit of an oddity in Apex. Apex doesn't have a notion of array so [] is only syntax sugar for list.get(i). When I rewrote the compiler I rewrote the grammar as well, keeping the good as well as the weird. I did make some improvements but looks like this one to be more consistent with ...


6

This sounds like a job for batch apex. Batch apex runs asynchronolously based on available resources and will chunk up your data per batch that i'll process. You should be good upto 50.000.000 records. Else you may want to export and do it externally, or use multiple batch iterations/processes depending on your specific scenario. Use the Database....


6

Batch Apex is absolutely the way to go for marking records as dirty and for the subsequent processing. When it comes to flagging them you could either set a specific field on each record, such as a checkbox, or potentially use a second custom object. In that object you'd create an entry for every record that needs processing (with a lookup to the first ...


6

You cannot search for wildcards at the beginning of a word, but this doesn't mean you can't search for any word anywhere within a long text area. Let's use an example string with some search terms: "The quick brown fox jumps over the lazy dog," said I. First, we need at least two characters, so we can't do this: FIND {I} ... Also, we can't search for "...


6

Can you try the following query select status, subject, whatId from task where NOT subject LIKE 'Email:%'


6

As mentioned in this post, you can perform LIKE on a list of matches. You might want to split on spaces and create a pool of candidates. It may be worthwhile to consume an extra query to try to find exact matches first. You will also probably get a better candidate pool if you use pair-wise neighbor combinations instead of individual split results. public ...


6

I know this is an old question, but I ran into the same problem today, and solved it like this: Select Id, ArticleNumber, Title from ArticleType__kav where Id not in (Select ParentId from ArticleType__DataCategorySelection) and PublishStatus = 'Online' You should replace ArticleType__kav and ArticleType__DataCategorySelection based on the name of your ...


6

Dynamic SOQL will not pass the security review. Your code is pretty safe because the end user has no possible way of injecting anything into the query. From here If you must use dynamic SOQL, use the escapeSingleQuotes method to sanitize user-supplied input. This method adds the escape character (\) to all single quotation marks in a string that is ...


Only top voted, non community-wiki answers of a minimum length are eligible