function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Radhika BansalRadhika Bansal 

How to use limit in the query run by Database.getQueryLocator?

I want to query to return more than 10,000 records and also do pagination. In my code we are using Database.getQueryLocator to query. 
The query is like 'Select Id from workorder where recordtype.name ='xyz' LIMIT 5000'

I debugged and found the query when used in Database.getQueryLocator, removes LIMIT keyword 
 for example 
query is >>SELECT Id,AIW_Activity_ID__c ,(Select Id, ParentId, Type, Title, Body, CommentCount,CreatedBy.Name, LikeCount, LinkUrl, RelatedRecordId From Feeds order by CreatedDate DESC LIMIT 5) FROM WorkOrder WHERE RecordTypeId = '0121l00000055y6AAA'  LIMIT 50000

but in database,getquerylocator it is - Aggregations:1|SELECT Id,AIW_Activity_ID__c ,(Select Id, ParentId, Type, Title, Body, CommentCount,CreatedBy.Name, LikeCount, LinkUrl, RelatedRecordId From Feeds order by CreatedDate DESC LIMIT 5) FROM WorkOrder 

Due to removal of  LIMIT keyword I am getting an error - System.LimitException: Too many query locator rows: 10001

I read that if we use database.getQueryLocator we can overlook the governor limit, but I guess that happens only when we use it in a  batch class.

Any idea how I can implement pagination with more than 10,000 records OR apply LIMIT clause in database.getQueryLocator.

The sample code is as below 
 System.debug('query is >>'+ query);
        Database.QueryLocator q = Database.getQueryLocator(this.query);
        this.dataIterator = q.iterator();
        return this;

 
PRAKASH JADA 13PRAKASH JADA 13

Hi,

You can use the LIMIT in this way in your Database.getQeryLoacation.
Declare:
String limitValue = 'LIMIT' + 5000;

Usage:
String query = 'Select Id from workorder where recordtype.name ='xyz'  + ' ' +limitValue;

Suggestion:
You are doing a hard coding in the query instead you can declare the strings separately and use them in you in your query.
Example:
String recordTypeName = 'xyz';
String limitValue = 'LIMIT' + 5000;

String query = 'SELECT Id FROM workorder '+
'WHERE recordtype.name = : recordTypeName' + ' ' +limitValue;

In this way, you can easily identify what are the variables you are using as a filter and can be user-readable. This is just a suggestion.

Thanks.