I got the error as heap size limit too large, I'm able to send below 2MB file by using Asynchronous method but the limits is 12MB and trying to send 12MB file to external system by using Rest Api.

Source Org

 public class SendAttachmentByRestAPI {
        private final String clientId = 'YYYY';
        private final String clientSecret = 'XXXXX';
        private final String username = 'abcd@gmail.com';
        private final String password = 'Password';
        public class deserializeResponse
            public String id;
            public String access_token;
        public String getAccessToken ()
            String reqbody = 'grant_type=password' + '&client_id='+clientId +
                            '&client_secret='+clientSecret + '&username='+username + '&password='+password;
            Http h = new Http();
            HttpRequest req = new HttpRequest();
           // req.setBody(JSON.serialize(reqbody));
            HttpResponse res = h.send(req);
            deserializeResponse response = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
            system.debug('@@@@access_token@@'+response );
            return response.access_token;
      @future(callout = true)
        public static void sendAttachement(){
            //get Access Token
            SendAttachmentByRestAPI accessTk = new SendAttachmentByRestAPI();
            String accessToken;
            accessToken = accessTk.getAccessToken(); 
            Set<Id> contentDocIds = new Set<Id>();
            for (ContentDocumentLink cdl : [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Opportunity WHERE StageName = 'Closed Won')]) {
            ContentVersion cvList = [SELECT Id,VersionData,Title,FileExtension, PathOnClient,ContentDocumentID FROM ContentVersion WHERE ContentDocumentId IN :contentDocIds limit 1];
            if(accessToken != Null){ 
                Http h = new Http();
                HttpRequest req = new HttpRequest();
                 String bodyEncoded = EncodingUtil.base64Encode(cvList.VersionData);
                    JSON.serialize(new map<String, String>{
                        'Title' => 'abc.jpg',
                        'VersionData' => bodyEncoded,
                        'FileExtension' => '.jpg'
                req.setHeader('Authorization','Bearer '  +accessToken);
                HttpResponse res = h.send(req);
              list<Document_URL__c> a = [SELECT Id,Name,Document_URL__c FROM Document_URL__c limit 1];
                for(Document_URL__c b : a){
                b.Document_URL__c = res.getBody();
                b.name = cvList.Title;
                update a;
            }  } 

My Trigger:

Trigger SendFile on Opportunity (after insert){
for(Opportunity a:Trigger.new) {
  • 2
    Please do not post actual credentials in questions
    – kurunve
    Aug 25, 2021 at 8:25
  • What is your question? Are you trying to send a 12MB file from Salesforce to an external service?
    – Alex
    Aug 25, 2021 at 9:29
  • Yes. I'm trying to send a 12MB file to an another Salesforce Org.
    – user102524
    Aug 25, 2021 at 9:32
  • The overhead of Base64 encoding is roughly 33.333%, so if the original size is 12MB the encoded one will be ~16MB. By the way, you should also take in account the memory required by the whole apex transaction, not only the file size.
    – RubenDG
    Aug 25, 2021 at 10:13
  • thanks for reply... I have tried to send the 5MB file ,but also same error in this it also covered the whole apex transaction memory
    – user102524
    Aug 25, 2021 at 11:06

1 Answer 1


I would try to debug my code using the apex code: Limits.getLimitHeapSize(); You can see the apex reference here: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_limits.htm#apex_System_Limits_getHeapSize

and here is an example of how someone else did it: Limits.getHeapSize() wildly inaccurate?

At least this debugging method will give you an idea of where the heap size error is occurring. Maybe you are calling the trigger with too many opportunities? Maybe like the comments say other areas are adding to the heap size.

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.