7

I need to create a huge document.

Naturally I face up the apex's limits such that heap size and string length.
I can split the creation of the document using a batch job. I thought to create the rows of this big document inside the execute method of the batch job and then write down only the new rows of the document avoiding the apex limits.

However this is useless because I always need to retrieve the document, retrieve its body append the new rows of the current execution and update the document.

global class exportContactsCampaigns implements Database.Batchable<sObject>, Database.Stateful {

    global void execute(Database.BatchableContext BC,  List<Contact> contactList){

        // create the rows for this execution
        String sExport = oExportHelper.buildExportString(contactList);

        // finished to elaborate the records, let's free some heap memory.
        contactList.clear();

        // update the document
        Document d = [SELECT Id, Body from document where Id = '01511000000OcXR'];
        String sCurrentBody = d.Body.toString();
        sCurrentBody += sExport; // PROBLEM!!! HEAP LIMIT OVERFLOW OR STRING LENGTH OVERFLOW
        d.Body = Blob.valueOf(sCurrentBody);
        update d;
    }
}

So my question is:
Is there a way to append the rows to a document without retrieving it's body?

4
  • Why do you need to create this document in Apex? (You'd be better off doing it almost anywhere else, and retrieving the data from SFDC using API calls.) – user735 Jan 22 '15 at 17:24
  • I have million of records to analyze, so I prefer doing it inside salesforce without the overhead of get all these data with api. – lastYorsh Jan 23 '15 at 8:18
  • @lastYorsh Did you find any solution or work around for this? I am having a similar requirement and have no clue how to achieve this. – Saumya Ranjan Satapathy Apr 4 '16 at 7:19
  • @SaumyaRanjanSatapathy: at the end I've managed with a workaround. I've created an external web service using some simple php code. At the end of each batch job I call that web service with the records exported. The web service appends the exported record in a file stored in a remote server, out from salesforce. I know, It is a really complex solution, but it was the only one I've found. – lastYorsh Apr 5 '16 at 8:19

Your Answer

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

Browse other questions tagged or ask your own question.