1

So I am trying to clone ~11000 attachments over to new object records. I have developed the code below with help from this community and I have tested it on a sandbox with 3 attachments to move over. Works great. But when I deploy it and trigger the code, I get the error below:

FATAL_ERROR System.LimitException: Query of LOB fields caused heap usage to exceed limit.

The Code:

trigger SecondStepClone on Opportunity (After update) {
    List<Attachment> ToInsertList = new List<Attachment>();  
    Attachment tempatt; 

    Map<Id, Id> newParentMap = new Map<Id, Id>();

    for(Opportunity curr: [SELECT ID, Selection_Record__c 
                            FROM Opportunity 
                            WHERE Selection_Record__c !='' AND (RecordTypeID = '012j0000000A8g3' 
                            OR RecordTypeID = '012j00000000Bvo' 
                            OR RecordTypeID = '012j00000000Bvj'
                            OR RecordTypeId = '012j000000114KI')]) {

        newParentMap.put(curr.Id, curr.Selection_Record__c);       
    }

    List<Attachment> attlist = [SELECT id, parentid, body, name 
                                FROM Attachment 
                                WHERE parentid 
                                IN: newParentMap.keyset()];  

    for(Attachment temp: attlist){
        tempatt=temp.clone(false,false);
        tempatt.parentid=newParentMap.get(temp.ParentId);  

        ToInsertList.add(tempatt);
    }    
    insert ToInsertList;
}

Not sure what next steps would be to prevent the heap size from being exceeded. Help would be much appreciated.

2 Answers 2

1

There's no need to clone the records, since you can just reset the id field by assigning null.

for(Attachment temp: attList) {
    temp.Id = null;
    temp.ParentId = newParentMap.get(temp.ParentId);
}
insert attList;
2
  • 1
    This will not work. You will receive the following if you try and update the ParentId on an Attachment. ERROR: System.SObjectException: Field is not writeable
    – Tad
    Apr 1, 2019 at 20:02
  • @Tad This looks like answer where I probably presumed that since the original code "worked", this should have worked, too. I'll have to revisit this answer when I get a moment.
    – sfdcfox
    Apr 1, 2019 at 21:20
1

This error is caused due to querying body from attachment.Remove body from the query to avoid the error.The updated code below

List<Attachment> attlist = [SELECT id, parentid,name 
                                FROM Attachment 
                                WHERE parentid 
                                IN: newParentMap.keyset()];  

Hope this helps!!

However please do the following to include the body and avoid the error:

for(Attachment temp: [SELECT id,body, parentid,name 
                                FROM Attachment 
                                WHERE parentid 
                                IN: newParentMap.keyset()]){
        tempatt=temp.clone(false,false);
        tempatt.parentid=newParentMap.get(temp.ParentId);  

        ToInsertList.add(tempatt);
    }    

This will avoid storing the records in a list and increasing the heap size

3
  • While it's probably true that querying the body of the attachment is a big contributor to running out of heap, an Attachment without the body isn't very useful.
    – Derek F
    Oct 20, 2016 at 13:10
  • thats right editing my answer.Thanks for pointing that out.didnt oversee it
    – Preya
    Oct 20, 2016 at 13:18
  • Thanks for the advice. Should of realized that I should of incorporated the query within the For loop. I still experience the same problem though. The insert list itself might be too large and exceeding the limit. I am going to attempt to break this down into multiple triggers by subgrouping the records through a field as simple as 1,2,3,4,5. If anybody has any better suggestions please let me know as this is a brute force approach which I save for last resorts. Oct 20, 2016 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .