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.
Best Answer
There's no need to clone the records, since you can just reset the id field by assigning null.