14 votes
Accepted

Exception: QUERY_TIMEOUT Your query request was running for too long

Take a look at Working with Very Large SOQL Queries. Querying for field != null will hurt your performance. Typically, a custom index isn’t used in these cases. The queried values exceed ...
Adrian Larson's user avatar
  • 150k
7 votes

Exception: QUERY_TIMEOUT Your query request was running for too long

Text field indexes do not index "null" values. In other words, the database can't reduce the cost for a query that filters for a null value in a text field. If most of your table has values, but a few ...
sfdcfox's user avatar
  • 491k
7 votes
Accepted

Where do queries [SELECT ...] return QueryLocator instead of List<SObject>?

Child relationships can return a QueryLocator. This can catch developers off-guard, because it requires relatively large amounts of data to happen. For example: for(Account acc: [SELECT (SELECT Name ...
sfdcfox's user avatar
  • 491k
7 votes
Accepted

Is there a typesafe way to get a Database.QueryLocator?

Provide hardcoded fields in query locator? I believe you can do this, this gives a compile-time check Database.getQueryLocator([SELECT Id FROM Account]); Src:https://developer.salesforce.com/docs/...
Pranay Jaiswal's user avatar
5 votes

Is there a typesafe way to get a Database.QueryLocator?

Partially. You can always establish static references to the involved fields and sObjects, and build the query dynamically with templating. I never like seeing queries built by string concatenation ...
David Reed's user avatar
  • 92.8k
5 votes
Accepted

Does an Inner Query in Query Locator count towards row governor limit?

Based on this answer: In a single transaction, you can have up to 300 parent-child subqueries. Each row returned by a parent-child subquery does count towards the 50,000 query row governor limit. ...
Matias Kruk's user avatar
5 votes

Query exception - Unexpected token '{'

There is no need to use dynamic SOQL in batch Apex (unless you have to). Because if you will do typo, and SOQL would be invalid, you will not see error message after code complying (saving to org). ...
Oleksandr Berehovskyi's user avatar
5 votes

Mocking Database.QueryLocator in Unit Tests using fflib Apex Mocks

Currently, I came up with some ugly but efficient solution for mocking Database.QueryLocator and to be precise, its substitution. To change the return type of AccountSelector.locatorByIds() method ...
Oleh Berehovskyi's user avatar
5 votes
Accepted

How to setup query on query locator

Custom Metadata Type are cached and using getInstance or getAll you retrieve just the first 255 characters of a text field, so long text area field might be truncated as stated in the documentation: ...
RubenDG's user avatar
  • 8,542
4 votes
Accepted

How to call QueryLocator.iterator() twice

The documentation has the following to say about iterator: To iterate over a query locator, save the iterator instance that this method returns in a variable and then use this variable to iterate ...
IllusiveBrian's user avatar
4 votes
Accepted

How to Delete Records in a object except Newly created 1000 Records by using Batch Class?

If you want to skip the newest 1000 records, use an OFFSET clause. Make sure you add an ORDER BY clause as well so that the newest records are those skipped. Since you can't use OFFSET in your query ...
Adrian Larson's user avatar
  • 150k
4 votes

Risks of replacing SOQL query with QueryLocator

There's no "risk" with using QueryLocator as compared to using a direct assignent. Your code would use (about) same amount of CPU time, but may use less overall memory, and has a reduced chance of ...
sfdcfox's user avatar
  • 491k
4 votes

Inconsistency with error "Aggregate query has too many rows for direct assignment"

Salesforce, from time to time, uses some not-so-clear wording and terminology. A perfect example is "Aggregate" queries. You might think that an "Aggregate" query is one that uses Aggregate functions ...
Derek F's user avatar
  • 61.6k
4 votes
Accepted

Exception thrown by StandardSetController with Database.getQueryLocator?

There is no need for a try-catch block your code, because there is nothing you can catch that's worth catching. There's governor limits that could be exceeded, which you can't catch, and the ...
sfdcfox's user avatar
  • 491k
3 votes
Accepted

Query more than 50000 records in a normal class

Since Simmer '18, The SOQL Count() Function Doesn’t Count (As Much) Toward Limits so you can count way more than 50,000 records. If you are working from a Visaulforce page, setting readOnly="true" ...
Keith C's user avatar
  • 136k
3 votes
Accepted

queryLocator - Use date variable

Two options: Create an instance of Date, based on today, then subtract 60 days from that date. AddDays is a bit misleading, since you can add a negative number of days to the date. Date.today()....
battery.cord's user avatar
  • 8,825
3 votes

Is there a typesafe way to get a Database.QueryLocator?

@PranayJaiswal has the best answer but you can also consider the Force.com Enterprise Patterns - Selector layer that uses a queryFactory and type safe fields as in: Database.QueryLocator ql = ...
cropredy's user avatar
  • 71.4k
3 votes

Batch process order of records in execute

tl;dr: yes. There's a few peculiarities when sorting by Created Date, partly due to the fact that the system assigns Id values and time stamps in a way that may cause them to be out of order when ...
sfdcfox's user avatar
  • 491k
3 votes
Accepted

Argument must implement iterable

String.join(...) expects an object which inherits the iterable interface as a first parameter, generally a list. Set does not implement the iterable interface, and as a result, you get this error ...
battery.cord's user avatar
  • 8,825
3 votes
Accepted

What kind of actions open a query locator

After a long support journey, it has been found out that there is internal salesforce code that get executed under specific conditions (depending on organization configuration), that can open a ...
lgraf's user avatar
  • 443
3 votes
Accepted

Variable not available for a batch query string

Lets expand your code: global Database.QueryLocator start(Database.BatchableContext BC) { String query = makeString(sources); return Database.getQueryLocator(query); } Becomes: global ...
Pranay Jaiswal's user avatar
3 votes

Does it make sense to use ORDER BY clause when it comes to batch `start()` method

No, that isn't quite what that section from the documentation is saying. If you ORDER BY your SOQL query in start(), the platform does not guarantee that the execution of batches will be in the same ...
David Reed's user avatar
  • 92.8k
2 votes
Accepted

illegal assignmet database.querylocator to list<campaign>

Database.getQueryLocator returns the List. So you just need to assign the the list of values like below : - global Database.QueryLocator start(Database.BatchableContext bc) { query = ...
Avijit Das's user avatar
  • 1,416
2 votes
Accepted

Unexpected token '(' error batch apex

You can't use any sort of dot reference in a dynamic bind variable. So anywhere you are calling .keySet(), you would need to cache that Set to be referenced directly. Invalid String query = '...WHERE ...
Adrian Larson's user avatar
  • 150k
2 votes
Accepted

Does QueryLocator work with Big Objects?

I have been struggling with the same, here is what I was told: The QueryLocator approach is not supported for any Virtual Entities that don't return a total size count. This is because BatchApex ...
PawelS's user avatar
  • 36
2 votes
Accepted

Batch Apex Chaining Limits?

As you are running a new batch. In batch Processing every execute get a new set of limits and it counts a different transaction. So all three will be counted as 3 transaction. Also one execute method ...
Tushar Sharma's user avatar
2 votes
Accepted

Batch class String query does not allow a set filter

You're mixing Apex bind variable syntax with string concatenation. You need to change this line: 'Product_Practice__c NOT IN :' + practiceFilter + ' AND ' + to '...
David Reed's user avatar
  • 92.8k
2 votes

Query exception - Unexpected token '{'

The reason for the error itself is that if you use string coercion to add a collection to the query it outputs as {...}. In other words: system.debug('' + new Set<String>{'A', 'B'}); // yields {...
Adrian Larson's user avatar
  • 150k
2 votes

Variable not available for a batch query string

When you use Apex binding in Dynamic SOQL, the bound variables must be in scope at the point when Database.query() is called, rather than at the point when the query string is initialized. Here, you'...
David Reed's user avatar
  • 92.8k
2 votes

ERROR:Unexpected token 'Quantity_Available__c'.,expecting a colon, found 'Quantity_Available__c'

You cannot compare two different field values in your SOQL 'where' clause. E.g. the below is not allowed. 'Quantity_Ordered__c >= Quantity_Available__c' What you can do to achieve your need is ...
EugeneO's user avatar
  • 41

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