You are querying too many unneeded records and creating Attachments for them which is why you are hitting the heap limit. Try make the 2 changes listed below **1: Restrict your query** Changing the SOQL to have extra where conditions will mean you are querying less records which means less fields that are base64 encoded. The SOQL below is looking at the Account_vod__c fields which will mean you query less records. [select Name,Id,Signature_vod__c,Signature_Date_vod__c,Account_vod__c from Call2_vod__c where Signature_Date_vod__c !=Null and Account_vod__c in :existMap.keySet()] **2: Change the for loop** Because you do your SOQL and store the results in `a` it means that ALL records will be on the heap. Try changing your for loop to the format shown below. This will avoid the heap issues are you are not storing all the results to a list: for (Call2_vod__c call : [select ... from ... where ...]) { // }