0

We are making a callout to external endpoint from Salesforce to get the attachments. when attachment size is more than 6 MB its throwing me Heap size limit error and we are running this callout from future method where we are having this limit as 12 MB.

Please advise ....

Code here ...

@future(callout=true)
    public static void SendAttachmentsToCase(Map<string,string> Initialres,ID CaseID,ID UserID,string UserName,string Password,string ticketNumber,string jobtime)
    {

                // Performs another callout to get appropriate attachment 
               List<attachment> FinalAttachmentList = new List<attachment>();
               for(string sg : Initialres.Keyset()) // Map of attachment Names and endpoint
               {
                Http h2 = new Http();
                HttpRequest req2 = new HttpRequest();
                Blob headerValue = Blob.valueOf(UserName+':'+Password);
                String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
                req2.setHeader('Authorization', authorizationHeader);
                req2.setHeader('Content-Type','application/json');
                req2.setEndpoint(Initialres.get(sg));
                req2.setMethod('GET');
                req2.setTimeout(60000);
                try
                {
                system.debug('heap size before req'+' '+ Limits.getHeapSize());
                HttpResponse res2 = h2.send(req2); // Hitting the error at this line. It shows me the heap size before the HTTP req
                system.debug('resposne'+ ' '+res2.getBody());
                                system.debug('heap size after req'+' '+ Limits.getHeapSize());

                blob image = res2.getBodyAsBlob(); 
                Attachment a = new Attachment (ParentId = CaseID,
                                           Body = image,
                                           OwnerId = UserID,
                                           CreatedById = UserID,
                                           Name = sg);
               FinalAttachmentList.add(a);
                }catch (Exception e)
                {
                    system.debug('exception message at 2nd callout level'+ ' '+ e.getMessage());
                 }
              }

              insert FinalAttachmentList;
    }
7
  • Is attachment more than 12MB? – Pranay Jaiswal Apr 17 '18 at 10:31
  • 12MB heap size is for the specific transaction the memory used for processing whereas the 3mb callout limit is for the request or response size. – RCS Apr 17 '18 at 10:32
  • Hi RCS, that limit of 3mb is 3 years old. Now its heap size. releasenotes.docs.salesforce.com/en-us/spring15/release-notes/… – Pranay Jaiswal Apr 17 '18 at 10:32
  • yes got it so the error is basically when you get the response body into blob it might be exceeding the heap size limit. – RCS Apr 17 '18 at 10:35
  • @ Pranay Jaiswal No attachment size is 6.12 MB – vamshi krishna Apr 17 '18 at 10:37
1

As I can see from your code, you are storing all attachment in a list and then inserting it. This behaviour is making you store and consume the heap limit bit more than your actual attachment size.

I know its ugly, but for this I recommend you doing a DML inside a for loop.

The memory scope of for-loop is per iteration. After completion of iteration the memeroy is garbage collected. Thus freeing your loop consumed memory.

If you un-bulkify your code it will help you save that attachment.

@future(callout=true)
    public static void SendAttachmentsToCase(Map<string,string> Initialres,ID CaseID,ID UserID,string UserName,string Password,string ticketNumber,string jobtime)
    {


               for(string sg : Initialres.Keyset()) // Map of attachment Names and endpoint
               {
                Http h2 = new Http();
                HttpRequest req2 = new HttpRequest();
                Blob headerValue = Blob.valueOf(UserName+':'+Password);
                String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
                req2.setHeader('Authorization', authorizationHeader);
                req2.setHeader('Content-Type','application/json');
                req2.setEndpoint(Initialres.get(sg));
                req2.setMethod('GET');
                req2.setTimeout(60000);
                try
                {
                system.debug('heap size before req'+' '+ Limits.getHeapSize());
                HttpResponse res2 = h2.send(req2); // Hitting the error at this line. It shows me the heap size before the HTTP req
                system.debug('resposne'+ ' '+res2.getBody());
                                system.debug('heap size after req'+' '+ Limits.getHeapSize());

                blob image = res2.getBodyAsBlob(); 
                Attachment a = new Attachment (ParentId = CaseID,
                                           Body = image,
                                           OwnerId = UserID,
                                           CreatedById = UserID,
                                           Name = sg);
               insert a;
                }catch (Exception e)
                {
                    system.debug('exception message at 2nd callout level'+ ' '+ e.getMessage());
                 }
              }


    }
7
  • tried that too but hitting the same error when making HTTP request. It shows me the heap size before the HTTP request but not after the request – vamshi krishna Apr 17 '18 at 10:41
  • Even tried removing the entire attachment code. But still it hits the Heap size limit error – vamshi krishna Apr 17 '18 at 10:46
  • Whats the last line that is executed, can you tell that adding debug logs? – Pranay Jaiswal Apr 17 '18 at 10:47
  • Last line HttpResponse res2 = h2.send(req2); 15:41:02:056 CODE_UNIT_STARTED 15:41:02:069 USER_DEBUG [22]|DEBUG|heap size before req 2257 15:41:02:069 CALLOUT_REQUEST [23]|System.HttpRequest[Endpoint=xxxxxxxx.com/attachment/1880476/…, Method=GET] 15:41:04:589 CALLOUT_RESPONSE [23]|System.HttpResponse[Status=OK, StatusCode=200] 15:41:05:629 EXCEPTION_THROWN [EXTERNAL]|System.LimitException: Apex heap size too large: 12937649 – vamshi krishna Apr 17 '18 at 10:52
  • Your response is more than 12MB that's what debug logs says. – Pranay Jaiswal Apr 17 '18 at 10:54

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.