Questions tagged [soql]
Salesforce Object Query Language (SOQL), modeled on SQL, for accessing information saved in the Salesforce database.
5,882
questions
125
votes
2
answers
152k
views
Is it possible to run a SOQL Query and get a Map returned?
It is possible to run query and the results inserted into an Apex list:
List<Opportunity> opportunities = [SELECT Opportunity.OwnerId,
Opportunity....
73
votes
4
answers
28k
views
Elegant way to convert Set<Id> into String for Dynamic SOQL IN comparison
When I have a Set of Id's and I want to do a dynamic SOQL query that I want to use it in for an IN comparison, I have to convert the Set to string in the format of:
('id1','id2',id3',...)
The way I ...
64
votes
1
answer
24k
views
Is there a way to do Like against a list of Values?
Is there a way to mix LIKE operator with IN ?
Something Like
SELECT Id FROM Account WHERE Name LIKE IN:nameList
Where nameList is list of String?
Is this is possible ?
(I can make a dynamic query by ...
57
votes
6
answers
31k
views
Can report data be accessed programatically?
Is there any way to access existing reports and their data to be used in apex? Sure, I could write the SOQL manually but if I want it to mirror a specific report I would have to make code changes ...
51
votes
7
answers
48k
views
Obtaining all ContentDocument records using SOQL
According to the Salesforce documentation on ContentDocument
Users (including users with the “View All Data” permission) can only query files they have access
I find this very strange. Why would a ...
51
votes
5
answers
19k
views
SOQL Record Locking : For Update
Well I was trying to Implement Record Locking apex but Salesforce Document doesn't seem to help much. Even tried searching stackexchange there was a old question but the answer form StackExchange and ...
49
votes
8
answers
84k
views
SOQL Alias a column
I am wondering how you would do something simple like this in SOQL.
select first_name as name from tableA
I can not find any documentation on Aliasing a column which is very common in all forms of ...
47
votes
6
answers
78k
views
Howto wildcard SELECT * to query all fields of objects in APEX?
I have created sObject and I need all the fields and I have many. Is there a way I can directly get all of them rather then typing each one of them.
46
votes
3
answers
25k
views
Workaround for Offset 2000 limit on SOQL Query
Problem
I'm trying to make a standard query pagination. I'm using SOAP API.
The logic is classic: LIMIT recordsByPage OFFSET (recordsByPage*pageNumber)+1,
It works fine, however, I'm facing the ...
44
votes
1
answer
132k
views
How do I build a SOQL query for records in a list of IDs?
Can some one correct my soql? I need to export records in data loader like:
WHERE Id IN ('123123123','123123fsdf')
But this following query does not work for me:
SELECT Id, Syndication_Documents__c ...
42
votes
3
answers
57k
views
SOQL vs SOSL - Which one to use and when?
What arguments can be made to use SOQL or SOSL? When would you use one over the other? What is the main difference?
41
votes
1
answer
10k
views
How many SOQL queries are spent for subqueries?
Apex Governor Limits docs state that:
In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query.
However, when a query with a single ...
39
votes
3
answers
52k
views
What standard and custom fields are indexed?
I'm trying to find a proper list of what standard fields are indexed by default in salesforce, as well as under what conditions custom fields are indexed.
To start things off I know fields are ...
39
votes
2
answers
11k
views
When To Use Set vs. List In SOQL
I have always assumed that it is better to use a Set in my query filters instead of a List. For example:
Set<Id> parentIds = generateParentIds();
List<Child__c> children = [SELECT Id FROM ...
38
votes
1
answer
660
views
Help me solve a concurrency mystery (platform bug? Expected behavior? Or am I missing something?)
To set up the problem, add a new currency data type custom field called Test_Counter__c to the Opportunity Object. Then create a test opportunity named TestOp.
Then create the following class:
...
33
votes
5
answers
27k
views
What is the maximum number of items in a SOQL IN clause?
Oracle has a limit of 1000 items for its IN clause. Is there a similar limit in SOQL? The SF documentation on the IN clause does not mention any limit. To clarify the definition of an item, the ...
30
votes
3
answers
45k
views
Use Set<Id> in dynamic SOQL IN-clause in method without Database.query()
I know and love that Apex is automatically converting Set to a valid SOQL string when it is used referenced like
Set<Id> ids = ...
String soql = 'SELECT ... WHERE Id IN :ids';
and passed to a ...
30
votes
4
answers
17k
views
Using SOQL apex bind variables for date literals (e.g. LAST_N_DAYS, TODAY, THIS_QUARTER)
Is there anyway I can use a variable value for LAST_N_DAYS. For example, I am trying to do something like:
Integer lastDays = 7;
//...
//...
List<Opportunity> opportunities = [SELECT OwnerId, ...
30
votes
1
answer
4k
views
What formulae does the Geolocation Distance use?
Forgive me if this is a bit extremely academic in nature, but I was wondering how Salesforce goes about determining the distance between two pairs of latitude and longitude?
When I've looked at this ...
30
votes
2
answers
768
views
When is SOQL Polymorphism going to be enabled by default?
SOQL Polymorphism (SELECT TYPEOF) was introduced in Winter 13, API version 26 for developer organization.
Is this feature going to be enabled by default for production organizations also sometime in ...
26
votes
3
answers
164k
views
Using a date for a datetime field in a SOQL Query [duplicate]
The SF doc explains how to put a Datetime query in a SOQL query.
SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08T01:02:03Z
I need to just put in a date for this query. Something like:
...
26
votes
2
answers
9k
views
custom setting getAll() versus SOQL
Does anyone have any background on which and when using the built in getAll() method for custom settings is better than just using traditional soql?
I am aware that getAll retrieves from the cache ...
25
votes
4
answers
64k
views
SOQL: Count number of child records
This is a basic SOQL question. I have a number of Application (custom) objects that are children to the User (standard) object.
I want to count the number of Applications associated with each User.
...
25
votes
3
answers
24k
views
Finding if which users have a record type available to them using SOQL
I would like to be able to execute a SOQL query that will allow me to see which users have access to a certain record type for a certain object.
For example, if I have a case record type called Phone ...
25
votes
3
answers
651
views
SObjectException No More. Intentional Change?
I found a pretty surprising change in platform behavior today.
Minimum Viable Reproduction (MVR)
Script
It's simple to reproduce. Just run this script in Execute Anonymous:
Account a = [SELECT Id ...
25
votes
1
answer
1k
views
SOQL bind variable scope behavior
After I posted this question, I found a similar post that seemed to answer it: Variable scope - database.query() has access to variables not in scope. I'll leave this in place for now, as I am not ...
25
votes
4
answers
2k
views
Batch start CPU timeout - What influences start() post-processing?
We're experiencing a very annoying issue with a Batch job processing "Large Data Volume" (1.5M records over a table containing 4.5M records). Our batch start method last statement is: return Database....
24
votes
2
answers
43k
views
Does LAST_N_DAYS:1 Include TODAY?
I would like to ask about the definition of LAST_N_DAYS:n
For the number n provided, starts 00:00:00 of the current day and
continues for the past n days.
Is
CreatedDate = LAST_N_DAYS:1
equal ...
23
votes
3
answers
50k
views
How to query NOT LIKE?
the soql in the following code is failing.
The error message is: "Compile failure on line 6, column 5: Unexpected token '<'."
Map<Id, String> profileIds = new Map<Id, String>();
...
22
votes
2
answers
2k
views
Why are Custom Settings lookups not counted against SOQL Query limits?
Custom setting documentation says "All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database" so does this mean ...
22
votes
2
answers
6k
views
SOQL query to load Task returns nothing, but I see task in UI
I am trying to load all tasks of a contact via SOQL query:
SELECT Id, Subject FROM Task WHERE WhoId = 'contactId'
This query returns only partial list of Tasks. I.e. in UI I see tasks directly ...
21
votes
2
answers
68k
views
How to query 'Value' of a picklist rather than the 'API NAME', using SOQL?
I am using SOQL to query the opportunity. When I use the query,
SELECT CUSTOM_PICK_LIST_COL__C FROM OPPORTUNITY
I am getting the values under the API Name of the picklist items. How to get the value ...
21
votes
5
answers
57k
views
Filtering by Long Text Area field in SOQL
I'm working on a custom search page, which will search through the fields of the Project object. I use dynamic SOQL query similar to that:
String query = 'SELECT Name, Status__c ...
FROM ...
21
votes
4
answers
7k
views
SOQL where clause use 15-character ID?
Can anyone confirm this: in a SOQL Where clause, when comparing an ID field to a 15-character string variable, the comparison works.
I didn't expect it to work, but it does and I am surprised. I can'...
20
votes
2
answers
39k
views
Query for records which have the city length > 20
I am looking for a way to query all leads which have the length of city__c > 20 characters in SOQL.
I cant seem to find anything like a length() in SOQL. Any workarounds? Or am i missing something ...
20
votes
3
answers
22k
views
Using a date for a datetime field in a SOQL Query criteria
Recently I had a strange bug in a project for a report page for closed cases. For whatever reason a case wasn't being included.
Long story short, I was querying for the cases using dates for the ...
19
votes
3
answers
35k
views
Apex: getting yesterday's date
I know that you get today's date like this: Date.today().
What would be the best solution to get yesterday's date?
19
votes
2
answers
2k
views
SOQL results returning RecordTypeId which is not included in query
I am running this script from developer console.
List<Account> lstAccount = [SELECT Id, Name, BillingStreet From Account LIMIT 1];
System.debug(lstAccount);
Debug Log:
23:47:04:012 ...
19
votes
3
answers
12k
views
SOQL Record Locking (FOR UPDATE)
I am looking for some more definitive and in depth information about record locking in Salesforce. I will admit I am quite a novice when it comes to the topic of record locking, and am not sure it is ...
19
votes
1
answer
45k
views
How to retrieve the file content for a ContentDocument object?
Previously, on Salesforce Classic, when a file was uploaded, it was saved on the Attachment object table. This object had the Body field.
On Lightning Experience, however, attachments are stored as ...
19
votes
4
answers
55k
views
Field can not be filtered in SOQL query call
Here is my SOQL code for finding records to update but it is generating a compiler error like "field 'Chatter_Monitor_Policy_Keyword__c' can not be filtered in query call". Why can't I filter by this ...
19
votes
3
answers
6k
views
Why is my SOQL query slow the first time, faster subsequently?
I am trying to understand how Salesforce executes a SOQL query. I have a query and when i run it for the first time (using Workbench, Developer Console or from the page) it takes more than a minute to ...
19
votes
4
answers
13k
views
Query Remote Site Setting?
Can we get all remote site settings in apex ?
I would like to check if a remote site setting exists or not.
18
votes
5
answers
28k
views
Use Name vs DeveloperName for RecordType?
What is best practice for working with RecordType in Apex (using DeveloperName)? Why should I use SOQL with DeveloperName if I can do it without query using regular name:
Schema.SObjectType.Account....
18
votes
1
answer
10k
views
Reference fields on parent object in aggregate query
I am trying to run a group by query in Apex where I am grouping by a field on a lookup object. For example:
SELECT Account.Type, count(Id)FROM Opportunity GROUP BY Account.Type
I can run this from ...
18
votes
2
answers
25k
views
Child-to-Parent Query - [object Object] is the result
I'm trying to perform the following multilevel relationship query.
SELECT ID, SystemModstamp, Product__r.Catalog__r.Active__c
FROM Journal__c
WHERE SystemModstamp > 2013-10-02T00:00:00.000+...
17
votes
4
answers
4k
views
How to find out if an Object is a collection or single value?
I have some dynamic SOQL code in which I'd like to identify whether a method argument is a collection or a single value:
SObject[] query(SObjectType sobType, String field, Object data) {
// Want ...
17
votes
5
answers
9k
views
how to get sandbox name in apex?
How to get the name of the sandbox in apex? I know how to determine if the environment is sandbox or production. Is there any metadata available in salesforce to find out the name of the sandbox that ...
17
votes
4
answers
6k
views
SOQL - query a query
We're in the process of converting an Access DB to Salesforce, a lot of our reports are built from querys of querys. For example, we want to produce a report that shows some fields from the parent ...
17
votes
1
answer
2k
views
Why is Query Plan not working in Developer Console in Winter '19 orgs?
When attempting to generate a Query Plan in Developer Console in a Winter '19 org, I receive a generic "sObject Type 'xxxxx' is not supported" error.
For example, when I use a basic query such as:
...