I am simply trying to do as title says. I had a solution originally that worked in only a batch of 1 but as this job is triggered by a TBF and sometimes works in batches it didn't correctly map the ParentId across and cloned every attachment into every custom object in the job.

I am trying to copy the solution from this post: Copying attachments from object to object trigger

However it clones the attachment to the original object not the newly inserted one.

public static void createRiskCheck(Risk_Check__c[] riskcheckcreate){

Schema.DescribeSObjectResult d = Schema.SObjectType.Risk_Check__c; 
rtMapByName = d.getRecordTypeInfosByName();
rtRiskCheck = rtMapByName.get('Risk Check');
rtInitialRiskCheck = rtMapByName.get('Initial Risk Check Creation');
Risk_Check__c newriskcheck;
List<Attachment> newattclist = new list<Attachment>();
list<Attachment> attachlist = new list<Attachment>();
list<Note> notelist = new list<Note>();
list<Note> newntelist = new list<Note>();
Set<Id> initialId = new Set<Id>();
List<Risk_Check__c> newriskchecklist = new list<Risk_Check__c>();

for(Risk_Check__c initialcreate : riskcheckcreate){
    newriskcheck = new Risk_Check__c();
    newriskcheck.Name = initialcreate.Name;
    newriskcheck.Reason_for_check__c = initialcreate.Reason_for_check__c;
    newriskcheck.Check_Due_Date__c = initialcreate.Next_Review_Date__c;
    newriskcheck.Client_Name__c = initialcreate.Client_Name__c;
    newriskcheck.member_number_new__c = initialcreate.member_number_new__c;
    newriskcheck.Superfund__c = initialcreate.Superfund__c;  
    newriskcheck.Initial_Risk_Check_Creation__c = initialcreate.Id; 
    newriskcheck.Related_Account__c = initialcreate.Related_Account__c;
    newriskcheck.Stage__c = 'To Be Assigned';
    newriskcheck.RecordTypeId = rtRiskCheck.getRecordTypeId();
//need to insert the new risk check first so we can use the Id when cloning any attachments on the initial create object 
insert newriskchecklist;

attachlist = [select Id, SystemModstamp, ParentId, OwnerId, Name, LastModifiedDate, LastModifiedById, IsPrivate, 
                       IsDeleted,  Description, CreatedDate, CreatedById, ContentType, BodyLength, Body from Attachment where ParentId in: initialId];

if(initialId != null && initialId.size() > 0){

    Map<Id,Risk_Check__c> riskcheckMap = new Map<Id,Risk_Check__c>([select Id,Initial_Risk_Check_Creation__c 
                                                                    from Risk_Check__c where Initial_Risk_Check_Creation__c in : initialId]); 

    List<Attachment> attachments = new List<Attachment>();

    for(Attachment file : attachlist){
        Attachment newFile = file.clone();
        if(riskcheckMap.containsKey(file.ParentId) && riskcheckMap.get(file.ParentId).Id != null){
            newFile.ParentId = riskcheckMap.get(file.ParentId).Id;
    insert attachments;

  • as per your code file.ParentId= riskcheckMap.get(file.ParentId).Id . SO which parent you want to assign the Id. Commented Sep 5, 2016 at 7:11
  • are you cloning record 1 to 1 or 1 record may have multiple clone record? Commented Sep 5, 2016 at 7:14
  • It's just one to one, I thought the Id in riskcheckMap.get(file.ParentId).Id was the newly inserted record is it not?
    – Michael
    Commented Sep 5, 2016 at 7:17
  • I would like ParentID to be the Id created from newriskchecklist. However it has to map specifically with the relative initialriskcheck which I have stored in a lookup field (Initial_Risk_Check_Creation__c) temporarily on newriskcheck.
    – Michael
    Commented Sep 5, 2016 at 7:26

1 Answer 1


What is needed here is a map from the Id in the riskcheckcreate list to the Id in the newriskchecklist list, most easily obtained like this:

Map<Id, Id> m = new Map<Id, Id>();
for (Integer i = 0; i < riskcheckcreate.size(); i++) {
    m.put(riskcheckcreate[i].Id, newriskchecklist[i].Id);

Then the Attachment parent Id can be swapped from old to new:

for(Attachment file : attachlist){
    Attachment newFile = file.clone();
    newFile.ParentId = m.get(newFile.ParentId);
insert attachments;
  • This worked and properly mapped the attachments across for batches with a greater size than 1 so now the attachments wont screw up with TBW.
    – Michael
    Commented Sep 6, 2016 at 0:00

You must log in to answer this question.

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