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.
multipart/form-data
. Search formultipart/form-data
in Rest Callout , you will get lot of answers in this forum also. Check this link for a head start.