+ Start a Discussion

heap size limit error on callout to external endpoint


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 .... on any workarounds to overcome this ..
    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);
                system.debug('heap size before req'+' '+ Limits.getHeapSize());
                HttpResponse res2 = h2.send(req2);
                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);
                }catch (Exception e)
                    system.debug('exception message at 2nd callout level'+ ' '+ e.getMessage());

              insert FinalAttachmentList;

Hi Saket,

Thanks I know we have this pilot program but I was looking out for other recommendations 
Hi Vamsi

If you don't mind can you please share from which third party you are integrating your system?
I'm getting Attachments from Zendesk