Wednesday, January 27, 2016

Salesforce - working with large paging sets greater then 2000


The Use Case : You have a visual force page/s that need to display large amounts of data, possible hundreds of thousands of records.

The Problem : SOQL limits and other force.com limits would prevent you from return large amounts of data to a page in one hit.

The Solution : To solve this problem we can make remote calls either synchronously or asynchronously to return the data in smaller chucks. This can be achieved by ensuring the following:

  •  passing your remote method the id of the last record in the last retrieved record set and using that as the starting point to retrieve the next set of records in your where clause.
  • Order the records by Id
  • Setting a limit of the number of records that are returned.


eg

@RemoteAction @ReadOnly
    public static List GetCostDistLines(String projectId, Id offsetId) // projectNum)
    {

        String offsetIdSearch = (offsetId==null)?'0':offsetId;

        return [select id
//, GLDate__c
                //, GLPeriodName__c
                , PADate__c
                , PAPeriodName__c
                , ExpenditureItem__r.Quantity__c
                , Name
                , ExpenditureItem__r.PersonId__c
                , ExpenditureItem__r.ExpenditureType__c, ExpenditureItem__r.Name
                , ExpenditureItem__r.VendorId__c
                , ExpenditureItem__r.UOM__c
                , ExpenditureItem__r.ProjectTask__r.Name
                , ExpenditureItem__r.ProjectTask__r.ProjectTaskNumber__c
                , ExpenditureItem__r.ProjectTask__r.Id
                from CostDistLine__c
                //where ExpenditureItem__r.ProjectTask__r.ProjectNumber__c = :projectNum];
                where ExpenditureItem__r.ProjectTask__r.pse__Project__r.Id = :projectId and
                Id > :offsetIdSearch ORDER BY Id ASC LIMIT 10000];
    }