Hot answers tagged

23 votes

Only variable references are allowed in dynamic SOQL/SOSL

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 = ...
user avatar
  • 4,972
11 votes
Accepted

Can we have a ORDER BY Clause in SOSL

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%'...
user avatar
  • 2,243
11 votes
Accepted

Does SOSL allow the `-` dash character?

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 ...
user avatar
  • 5,258
9 votes
Accepted

What is the default sort order for records in SOSL

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 ...
user avatar
  • 439k
8 votes
Accepted

Does SOSL use Caching?

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.
user avatar
  • 439k
8 votes

How to query child to parent

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....
user avatar
  • 22.1k
8 votes
Accepted

Using SOSL with where clause

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, ...
user avatar
  • 2,253
7 votes

How do I SOSL across objects, matching all records with FIND clause?

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) ...
user avatar
7 votes
Accepted

Is there a way to search a long text area for contains string in SOSL

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: ...
user avatar
  • 439k
7 votes
Accepted

Function to properly escape dynamic SOSL input

I just had to work on this problem too. I took a slightly different approach and let regular expressions do the bulk of the effort via the Matcher.replaceAll( string ) method instead of looping and ...
user avatar
  • 3,986
7 votes

Only variable references are allowed in dynamic SOQL/SOSL

The dynamic SOQL documentation explains that simple bind variables (introduced with a :) can be used, that is a reference to a variable (but not a dotted expression or method call). So the pattern to ...
user avatar
  • 131k
7 votes
Accepted

I wanna run my SOSL to search for a string obtained by function parameter

I'm pretty sure there's a duplicate out there somewhere, but the answer is you have to bind to the query: List<SObject[]> searchList = [FIND :param IN ALL FIELDS RETURNING Contact(FirstName, ...
user avatar
  • 439k
7 votes

Question about setFixedSearchResults() Apex Tests with SOSL

The latest call to Test.setFixedSearchResults overwrites whatever happened before. The comment is only meant to state that you can simulate multiple SOSL searches in a single unit test. Of course, ...
user avatar
  • 439k
7 votes

Array notation for List<List<Sobject>>

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 ...
user avatar
  • 71
6 votes
Accepted

Filter results with not like

Can you try the following query select status, subject, whatId from task where NOT subject LIKE 'Email:%'
user avatar
  • 1,026
6 votes
Accepted

SOQL: Bulk Fuzzy Searching

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 ...
user avatar
  • 144k
6 votes
Accepted

How to query for articles without a data category?

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 ...
user avatar
  • 96
6 votes
Accepted

Getting SOQL/SOSL Injection error when I send my code for security review

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 ...
user avatar
  • 12.2k
6 votes
Accepted

SOSL Search Results incomplete?

It's working as expected. Wildcards at the beginning of the word are not directly supported. From FIND {SearchQuery}, there's a specific note: Asterisks match zero or more characters at the middle ...
user avatar
  • 439k
6 votes
Accepted

SOSL does not find numeric fields

Correct. Per the Developer's Guide, SOSL is for "text-based search queries". On the FIND page, it also says in many ways that it is for words or phrases. If you have a number inside of a text field, ...
user avatar
6 votes

can we query SOSL using REST API?

Yes you can do SOSL query using REST Api. The api would be - /services/data/v37.0/search/?q=SOSL search string The SOSL search string needs to be URL encoded. Here is a link for more details - SOSL ...
user avatar
6 votes

Not getting a value in SOQL

Format your input variable with like operator and then use it in SOQL. Refer below code: MVImportBatch__c record = [select Id from MVImportBatch__c limit 1]; string cid = string.valueOf(String....
user avatar
  • 3,811
6 votes
Accepted

SOSL not returning record in test

SOSL doesn't return results in test classes, so you have to use special methods to set the results. To ensure that test methods always behave in a predictable way, any Salesforce Object Search ...
user avatar
  • 12.9k
6 votes

How to get list of all sobject records modified today in the system?

Simpler way? well yes and no. We have Database.getUpdated method that returns Id of records updated in a particular time span. It takes sObject Type , start date and end date as arguments and returns ...
user avatar
6 votes
Accepted

SOQL query to fetch records with reserved characters ('*', '/', '(', ')')

The * character is not a special character as far as SOQL is concerned. Here's a trivial proof for you: And here's the same in Apex: System.debug( Database.query( 'SELECT Name FROM ...
user avatar
  • 439k
6 votes
Accepted

How to return List of a list in Apex Rest Get Method Using SOSL

Don't mix responseBody and a return value. Use one or the other. It's a lot easier to use the return value, and offers more functionality for no extra effort, such as supporting XML and JSON input and ...
user avatar
  • 439k
5 votes
Accepted

What's the correct syntax for this SOSL sent via the REST API?

You are using (, ) and - in your search term, all of which are reserved characters in SOSL. From the documentation: The following characters are reserved: ? & | ! { } [ ] ( ) ^ ~ * : \ " ' + ...
user avatar
  • 13.3k
5 votes

Query Salesforce to find any records created in past "X" hours

You can run a code like this in execute anonymous. This code deletes accounts created in the past 30 hours: Datetime d = datetime.now().addhours(-30); List<Account> accounts = [SELECT Id FROM ...
user avatar
  • 2,172
5 votes
Accepted

How to Query what Salesforce SObjects a user has access to

The code below could be used to iterate over all SObjects in your org and then check to see if they are accessible. for (SObjectType so : Schema.getGlobalDescribe().Values()) { ...
user avatar
  • 12.2k
5 votes
Accepted

Find all records referencing a specific Account

You can find all the child relationships with a little Execute Anonymous: Schema.DescribeSObjectResult describe = Account.sObjectType.getDescribe(); for (Schema.ChildRelationship child : describe....
user avatar
  • 144k

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