I want to create a quick action button that call to apex class that execute a batch(I used a batch because I want to retrieve More than 50000 records and I have salesforce limitation). I want to take the list that I get from the batch and export it to csv file. I created apex method that has a list and the batch update that list I want to retrieve this list to client side (lighting) and export this data to csv file can someone have an idea how to do this request with the best way? this is my code: Apex class:

public with sharing class PRT_EntitlementTableCtrl { 
    public static List<Entitlement> element{get;set;}

    public static List<Entitlement> getRecordsForTable(Map<String, String> filters){
        element = new List<Entitlement>();
        String whereClause = getWhereClause(filters);
        PRT_Batch_Entitlement shn = new PRT_Batch_Entitlement(whereClause); 
        return element; 

Batch job:

public class PRT_Batch_Entitlement implements Database.Batchable<sObject>{
    public String query{get;set;}
    public String whereClause;

    public PRT_Batch_Entitlement(String strParam){
        whereClause = strParam;

    public Database.QueryLocator start(Database.BatchableContext BC){ 
        System.debug('start batch');
        System.debug('param : '+ whereClause);
        query = 'SELECT ID, Legacy_End_User_Company__c, Asset_Reference_WF__c, Asset_CPU_ID__c, Service_Name__c, Status, EndDate, ServiceContractSalesOrder__c, AssetSalesOrder__c ' +
                        'FROM Entitlement '+
                        whereClause + 'Limit 200';
        System.debug('query : '+ query);
        return Database.getQueryLocator(query);
     public void execute(Database.BatchableContext BC, List<sObject> scope){
        System.debug('execute batch');
        System.debug('scope : '+scope);
        for(sObject obj : scope){
     public void finish(Database.BatchableContext BC){
        System.debug('finish batch');

  • 1
    why not have the batch job create the CSV and save as a ContentVersion or Document and then fetch that file? The batch job could publish a Platform Event that the client subscribes to (which would contain the ID of the persisted CSV)
    – cropredy
    May 21 '19 at 0:21

Structurally, this is not going to work. Lightning Apex controllers don't have persistent state; those static variables reset at the boundary of every transaction. Each batch execute() method (and each Lightning server call!) is a separate transaction. Even if this weren't the case, you can't affect the state of an open UI session from unrelated backend processes like batch jobs anyway.

You'll need to have your JavaScript controller iteratively query for your records. Since you'll be going well past the 2000-record limit for an OFFSET clause, you'll have to ORDER BY Id, LIMIT 50000, and use a WHERE Id > :lastIdReceived to paginate through the data. This approach is described (in Apex) in an answer by sfdcfox here; you'll need to implement the iteration and state management in JavaScript in your client controller and the query itself in Apex in your server controller

You may be able to request 50,000 rows at a time this way - there's no documented limit on the response body size for a Lightning server controller request as far as I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.