1

We have a process in which we need to send a JSON of leads every day to an external system (Not Salesforce).

Until now we just created the JSON with the desired data and sent it via REST.

From time to time the amount of the Leads is changing - can be from 10 to 500 on "hot days"

A new requirement arrived which we need to send the Files of the Lead also in the JSON.

A Lead can have ~2 Files.

If I'll try to send the data using REST when adding the BLOB file - It can break the heap size limit.

this line: Blob.valueOf(EncodingUtil.base64Encode(VersionData)) to send the version data of the file, creating a very long text (11 pages of a .doc file - and I believe that for bigger files it will be longer).

my code: (I ignored here the details of the lead and just tried to load files to the JSON)

Set < Id > docIdSet = new Set < Id > ();
for (ContentDocumentLink doc: [SELECT ContentDocumentId,
        ContentDocument.Title, ContentDocument.LatestPublishedVersionId
        FROM ContentDocumentLink WHERE LinkedEntityId IN: leadIds
    ]) {
    docIdSet.add(doc.ContentDocumentId);
}



List < ContentVersion > contentVersionList = [SELECT id, ContentDocument.Title, ContentDocumentId, ContentDocument.LatestPublishedVersionId,
    ContentDocument.FileType, ContentDocument.ContentSize, VersionData, Title, TextPreview
    FROM ContentVersion
    WHERE contentdocumentid IN: docIdSet
];

List < NotesAndFiles > noteAndFileList = new List < NotesAndFiles > ();
for (ContentVersion c: contentVersionList) {

    NotesAndFiles naf = new NotesAndFiles();
    if (c.ContentDocument.FileType != 'SNOTE') {
        naf.type = 'File';
        naf.file = Blob.valueOf(EncodingUtil.base64Encode(c.VersionData));

    } else {
        naf.type = 'Note';
        naf.noteTitle = c.Title;
        naf.noteBody = c.TextPreview;
    }


    noteAndFileList.add(naf);

}



System.debug(JSON.serializePretty(noteAndFileList));

try{
HttpRequest httpReq = new HttpRequest();
httpReq.setEndpoint('endPoint');
httpReq.setMethod('POST');
httpReq.setHeader('Content-Type', 'application/json');
httpReq.setBody(JSON.serializePretty(noteAndFileList));
HttpResponse res  = new Http().send(httpReq);
}catch(Exception e){

    System.debug(e.getMessage());
}
    
    }






private class NotesAndFiles {
    String type;
    String noteTitle;
    String noteBody;
    Blob file;
}

Maybe sending it with REST is not the best practice here?

Thank you.

3
  • Sending file via Apex callout is little tricky. Salesforce doesn't support native multipart/form-data. Search for multipart/form-data in Rest Callout , you will get lot of answers in this forum also. Check this link for a head start.
    – Saroj Bera
    Commented Feb 3, 2022 at 8:07
  • Thank you @SarojBera , this does help, but I still have an issue with sending a large file (large enuff to break the heap limits), and with a few files that are total size break the limit (same concept). What is the best way to send those files if not via REST?
    – Salvation
    Commented Feb 6, 2022 at 9:21
  • I am not aware of any workaround to bypass this limit. You can think of doing this outside of Salesforce like Heroku, where the Data can be pulled from Salesforce then process and send to other System. Also you can explore new Salesforce Functions.
    – Saroj Bera
    Commented Feb 6, 2022 at 17:14

0

You must log in to answer this question.

Browse other questions tagged .