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
106
votes
2answers
89k 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....
71
votes
4answers
25k 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 ...
58
votes
1answer
15k 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 ...
54
votes
6answers
27k 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 ...
47
votes
4answers
15k 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 ...
43
votes
7answers
21k 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 ...
41
votes
6answers
52k 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 ...
40
votes
3answers
48k 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?
40
votes
3answers
18k 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 ...
39
votes
1answer
8k 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 ...
38
votes
5answers
44k 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.
38
votes
1answer
629 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: ...
37
votes
3answers
32k 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 ...
37
votes
1answer
64k 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 ...
35
votes
2answers
7k 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 ...
30
votes
3answers
39k 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
1answer
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
2answers
733 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 ...
28
votes
3answers
17k 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 ...
27
votes
4answers
13k 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, ...
25
votes
3answers
582 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
1answer
737 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
3answers
1k 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
3answers
12k 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 ...
23
votes
3answers
111k 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: ...
23
votes
2answers
7k 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 ...
21
votes
4answers
5k 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'...
21
votes
1answer
4k 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 ...
20
votes
2answers
1k 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 ...
19
votes
2answers
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
3answers
20k 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
2answers
17k 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 ...
17
votes
3answers
23k 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?
17
votes
3answers
37k 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. ...
17
votes
3answers
4k 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 ...
17
votes
4answers
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
4answers
7k 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.
17
votes
1answer
260 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: ...
16
votes
2answers
32k 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 ...
16
votes
4answers
31k 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 ...
16
votes
3answers
10k 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 ...
16
votes
2answers
10k views

How do I delete records in the recycle bin?

I understand that when I perform a Database.delete() operation, records will end up in my org's recycle bin. And their presence in the recycle bin is reflected by the IsDeleted field on the object. ...
16
votes
3answers
22k 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 ...
16
votes
2answers
18k 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+...
15
votes
2answers
18k 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 ...
15
votes
4answers
2k 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 ...
15
votes
5answers
14k 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....
15
votes
2answers
16k views

Trouble using SOQL to filter results by a range of Dates - What is the correct syntax?

Using the Salesforce API, I am trying to search for Cases which have a CreatedDate between two specific dates, but I am getting errors when sending my query. For example: SELECT Id FROM Case ...
15
votes
4answers
6k views

System.Exception: Too many SOQL queries: 101 [duplicate]

I am getting a Too many SOQL queries: 101 error on a trigger when I am trying to deploy another trigger. I am posting the trigger which is causing the error. trigger rollupAccountShowings on Account ...
15
votes
1answer
7k 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 ...

1
2 3 4 5
99