Skip to main content

Questions tagged [soql]

Salesforce Object Query Language (SOQL), modeled on SQL, for accessing information saved in the Salesforce database.

Filter by
Sorted by
Tagged with
126 votes
3 answers
165k 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....
dublintech's user avatar
  • 4,273
74 votes
4 answers
29k 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 ...
pchittum's user avatar
  • 19.7k
66 votes
1 answer
26k 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 ...
Avidev9's user avatar
  • 5,659
58 votes
6 answers
32k 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 ...
Ryan Elkins's user avatar
  • 7,687
54 votes
7 answers
52k 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 ...
GuiSim's user avatar
  • 962
51 votes
5 answers
20k 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 ...
Avidev9's user avatar
  • 5,659
49 votes
8 answers
93k 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 ...
Josh's user avatar
  • 629
48 votes
3 answers
27k 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 ...
Martin Borthiry's user avatar
47 votes
6 answers
81k 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.
NewBei's user avatar
  • 473
45 votes
1 answer
142k 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 ...
sfdc's user avatar
  • 13.7k
42 votes
3 answers
58k 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?
Jesse Altman's user avatar
  • 19.7k
41 votes
1 answer
11k 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 ...
ipavlic's user avatar
  • 3,736
39 votes
3 answers
54k 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 ...
ca_peterson's user avatar
39 votes
2 answers
12k 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 ...
Adrian Larson's user avatar
  • 150k
38 votes
1 answer
669 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: ...
kibitzer's user avatar
  • 3,444
33 votes
5 answers
29k 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 ...
edgartheunready's user avatar
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 ...
Robert Sösemann's user avatar
30 votes
4 answers
18k 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, ...
dublintech's user avatar
  • 4,273
30 votes
1 answer
5k 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 ...
Daniel Ballinger's user avatar
30 votes
2 answers
779 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 ...
Tzach's user avatar
  • 684
28 votes
2 answers
70k 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>(); ...
Dev Arya's user avatar
  • 545
26 votes
3 answers
176k 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: ...
dublintech's user avatar
  • 4,273
26 votes
4 answers
68k 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. ...
powlo's user avatar
  • 2,660
26 votes
2 answers
10k 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 ...
dacology's user avatar
  • 2,992
25 votes
3 answers
27k 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 ...
Joe's user avatar
  • 3,421
25 votes
3 answers
663 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 ...
Adrian Larson's user avatar
  • 150k
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 ...
Johnathan Ashley's user avatar
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....
MLucci's user avatar
  • 1,098
24 votes
2 answers
49k 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 ...
Pawel Gumiela's user avatar
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 ...
Shebin Mathew's user avatar
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 ...
antonlitvinenko's user avatar
21 votes
2 answers
73k 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 ...
Kannan Ramamoorthy's user avatar
21 votes
5 answers
62k 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 ...
Hleb's user avatar
  • 397
21 votes
2 answers
45k 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 ...
Prady's user avatar
  • 12.3k
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'...
David Cheng's user avatar
  • 7,091
20 votes
4 answers
62k 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 ...
Kishan Trambadiya's user avatar
20 votes
3 answers
23k 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 ...
Ralph Callaway's user avatar
19 votes
3 answers
37k 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?
justasd's user avatar
  • 1,463
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 ...
Santanu Boral's user avatar
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 ...
Daniel Blackhall's user avatar
19 votes
1 answer
48k 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 ...
Renato Oliveira's user avatar
19 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 ...
hamayoun's user avatar
  • 2,777
19 votes
3 answers
7k 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 ...
Sahil's user avatar
  • 391
19 votes
2 answers
26k 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+...
dannymorty's user avatar
19 votes
4 answers
14k 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.
Eric's user avatar
  • 450
18 votes
5 answers
31k 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....
Vladyslav K's user avatar
  • 2,743
18 votes
5 answers
10k 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 ...
Sathya's user avatar
  • 3,232
17 votes
7 answers
131k views

How to select Unique values in SOQL

I am trying to get unique values by Territory_ID__c with a (Salesforce SQL) SOQL query. I tried this: return [select id,name,ANNUAL_CALLS__c,city__c,state__c,No_Of_Targets__c, Territory_ID__c,...
Arun SFDC's user avatar
  • 1,458
17 votes
4 answers
5k 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 ...
Keith C's user avatar
  • 136k
17 votes
3 answers
7k views

SOQL result ordering in the absence of an Order By clause

In the absence of an Order By clause in a SOQL query, what order will Salesforce return results in? Clearly if ordering is important then Order By should be specified, but I recently encountered a ...
Daniel Ballinger's user avatar

1
2 3 4 5
120