Skip to main content
22 votes
Accepted

Select into Map using Database.query()

It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right. How about: Map<Id, Account> accounts = new Map<Id, Account>((List<Account&...
Charles T's user avatar
  • 11k
18 votes
Accepted

How to find out if an Object is a collection or single value?

If you are using a data binding, you do not need to know. = will work in either case, so you don't need to ever switch to IN for the use case you outline above. I used the following code in Execute ...
Adrian Larson's user avatar
  • 151k
16 votes
Accepted

Check if an SOQL query is valid without actually executing it

You might be able to accomplish this by executing the query given with an additional LIMIT 0 at the end. You'll still use one SOQL query, but I don't believe this will count against the total number ...
IllusiveBrian's user avatar
13 votes

Possible to use bind :variable inside SELECT clause?

You have to use dynamic SOQL if you want to add fields dynamically. Merge syntax is not supported. List<String> fields = new List<String> { 'Id', 'Name' }; String soql = 'SELECT ' + ...
Adrian Larson's user avatar
  • 151k
12 votes

How to find out if an Object is a collection or single value?

Another alternative might be this: public static Boolean isCollection(Object input) { Object test; try{ test = (List<Object>)input; return true; } catch(System....
IllusiveBrian's user avatar
12 votes
Accepted

How to query the data that has been modified in last 30 MINUTES

The easiest way might be to make the following Minutes_Since_Modified__c formula: (NOW() - LastModifiedDate) * 24 * 60 Then to query for it: SELECT Id FROM Account WHERE Minutes_Since_Modified__c <...
Adrian Larson's user avatar
  • 151k
12 votes

Dynamic Query - System.QueryException: expecting a colon, found '.'

Explanation The core problem here is that implicit type conversion from List<String> to String will cause truncation. It looks like this happens after the tenth element. You can see it for ...
Adrian Larson's user avatar
  • 151k
11 votes
Accepted

Using single quote by curly braces in String.format in Dynamic SOQL

Quoting the merge field makes it literal text. In order to get the merge field to work, you have to quote the quotes: String query = String.format('SELECT Id FROM Availability WHERE Person__c = \'\'{...
sfdcfox's user avatar
  • 497k
11 votes

Select into Map using Database.query()

If you cast it to a List of the expected sObject type first it will work. E.g. Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name ...
Daniel Ballinger's user avatar
10 votes
Accepted

System.QueryException: expecting a colon, found 'setids'

When you reference an apex variable in a query (standard, or dynamic as is your case), you need to prefix the variable name with a colon :. This is called expression binding. You can still use ...
Derek F's user avatar
  • 62.7k
10 votes

Select into Map using Database.query()

Adding a bit of info here on top of other answers. This works: Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]); Because the return type from the SOQL is ...
Jayant Das's user avatar
  • 30.6k
9 votes
Accepted

Is IN operator equivalent to Equal operator in SOQL when binding a list variable?

Yes, = and IN operators are functionally equivalent when filtering against a list. It is not well covered in the Force.com SOQL and SOSL Reference for Comparison Operators, but the operators are ...
Adrian Larson's user avatar
  • 151k
9 votes
Accepted

build dynamic SOQL condition

What I normally do is construct some Set and store there all the possible values you want to compare against. Set<String> setemailsname = new Set<String> {'[email protected]','test1@gmail....
Alexander Aeons Torn's user avatar
9 votes
Accepted

Database.Query Variable Does Not Exist

The only field you can directly retrieve from an sObject is Id. All other fields must be referenced dynamically: pickListValuesList.add(new RelatedObject(s.Id,(String)s.get('Name'))); This is ...
sfdcfox's user avatar
  • 497k
9 votes

Does the `getSObjects` method on `SObject` work with Person Account child relationships?

Ok, I'm guessing Salesforce might hide their known issues from being crawled by search engines. Typed in getSObjects into the success community and the first thing that appeared was a known issue ...
Matt Lacey's user avatar
  • 25.7k
9 votes
Accepted

Cannot access the Parent field values from list<sObject> and Database.query

In order to get a parent sObject, you need to use getSobject, and to get a field from that, use get: for(sObject s : results){ system.debug(s.getSObject('Contact').get('Name')); system.debug(s....
sfdcfox's user avatar
  • 497k
8 votes
Accepted

no viable alternative at character '\'

As noted in the comments, you've escaped the wrong single quote. Incorrect 'WHERE Id = \'' + someId + \'' ' Correct 'WHERE Id = \'' + someId + '\'' Also noted in the comments, you can use bind ...
Adrian Larson's user avatar
  • 151k
8 votes
Accepted

Group by part of string in SOQL

If you really want to use specific field values in GROUP BY then create a text field and update the text field either by workflow rule or Process builder or trigger. Formula field is not supported ...
Santanu Boral's user avatar
8 votes
Accepted

Should the user input be escaped in dynamic SOQL when using binding?

The short answer is no. Because you are using a binding there is no possibility for the value to introduce an SOQL injection attack. It is only when you are textually inserting the value into a ...
Phil W's user avatar
  • 37k
8 votes

Issue with dynamic SOQL Query

In Dynamic SOQL, you need to pay attention to spaces. There should be no space between : and what follows. Bad id NOT IN : setclosedId Good id NOT IN :setclosedId Spacing is important when ...
sfdcfox's user avatar
  • 497k
8 votes
Accepted

Dynamic SOQL Error: Unexpected Token ':' - I have searched for two days, what could I be missing?

The problem lies here: SELECT :queryFields You can use bind variables only in the WHERE clause. Replace this with the actual set of fields.
Mariia Illarionova's user avatar
7 votes

How to find parent records with only 1 child record

Query on Contact using Aggregate function Count() SELECT AccountId, count(Id) FROM Contact GROUP BY AccountId HAVING count(Id) = 1
Ishwar Mete's user avatar
  • 1,492
7 votes
Accepted

Convert string to query

Normally you could just use Database.query(queryString) to make a query from a string. However, that method only returns a List<SObject> that cannot be cast to a Integer. Since you are looking ...
Fernando Gavinho's user avatar
7 votes
Accepted

Error encountered with usage of != or <> operating for SOQL query in batch class

If you want to use a not equals operator on a text field, you must use a single, quote wrapped value. Parentheses indicate a collection and will only work with IN or NOT IN operators. A single value ...
Adrian Larson's user avatar
  • 151k
7 votes
Accepted

If I Use a Loan Pattern is Salesforce smart enough to do a SOQL for loop?

Agreed with sfdcfox that returning a List<SObject> will materialize the whole query first rather than let you do a proper SOQL FOR loop. One alternative is, you could have your factory return ...
Charles T's user avatar
  • 11k
7 votes
Accepted

Is "FOR UPDATE" available in dynamic SOQL?

You can't test record locking in testmethods so you'll need to use Execute Anonymous and some simulator that forces the FOR UPDATE locking condition I borrowed from Dan Appleman's 4th Edition Advanced ...
cropredy's user avatar
  • 72.1k
6 votes

batch class is failing due to time while processing SOQL due to timeout error

There are a number of things you can do to improve the performance of your SOQL query. The most effective will be to understand how to diagnose it yourself, because how to solve your issue depends on ...
Nick C's user avatar
  • 6,790
6 votes

how to escape wild character _ underscore in LIKE SOQL Clause

In my tests, a double backslash escape has provided the desired results: List<User> users = (List<User>)Database.query('Select Id, Email, ContactId, Username from User where Username ...
Fernando Gavinho's user avatar
6 votes

Salesforce Relational Query

Via the lookup relationship [Select objb__r.objc__r.field__c from objA__c] Otherwise you could do two queries and collections
Eric's user avatar
  • 54.2k
6 votes

no viable alternative at character '%' in dynamic soql

This isn't a "dynamic" query. A dynamic query is one that has variable fields, objects, or filter conditions. Unless you have a strong, compelling reason, simply use an inline query: SOject[] results ...
sfdcfox's user avatar
  • 497k

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