function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
imran Rahmanimran Rahman 

Using apex to copy attachment from custom object to Opportunities

I have a custom object which is mirrored to Opportunities with apex, a Opp foreign key field is then left in a field in my custom object.
How can I create a trigger which on attachment uploads to my custom object, they are copied to the relevant opportunity?
AshwaniAshwani
I can't write a complete code but what you can do is you will have the foreign key field value in trigger. So by this you can fetch attachemt by using parentId field. Keep these values in a list and also Ids of custom object. I prefer to use Map instead and create new Attachment a setup then setup paentId as custom object Id and copy other values like body, name etc.

Hint:
List<Attachment> attachmnt = [SELECT id FROM Attachment WHERE parentId in:LIST_OF_FOREIGN_KEYS];
// you don't need to use "Body" field of attachment. Only create a new Attachment instance and set ParentId as foreign key
Attachment newAtt = new Attachment(ParentId=Custom_Object_Id);
// other stuff related to attachment


imran Rahmanimran Rahman
This is the code I have created but it seems to be bring up an error:
Error: Apex trigger CopyAttachmentstoOpps caused an unexpected exception, contact your administrator: CopyAttachmentstoOpps: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CopyAttachmentstoOpps: maximum trigger depth exceeded Attachment trigger event AfterInsert for [00Pc0000001KBIj] Attachment trigger event AfterInsert for [00Pc0000001KBIk] Attachment trigger event AfterInsert for [00Pc0000001KBIl] Attachment trigger event AfterInsert for [00Pc0000001KBIm] Attachment trigger event AfterInsert for [00Pc0000001KBIn] Attachment trigger event AfterInsert for [00Pc0000001KBIo] Attachment trigger event AfterInsert for [00Pc0000001KBIp] Attachment trigger event AfterInsert for [00Pc0000001KBIq] Attachment trigger event AfterInsert for [00Pc0000001KBIr] Attachment trigger event AfterInsert for [00Pc0000001KBIs] Attachment trigger event AfterInsert for [00Pc0000001KBIt] Attachment trigger event AfterInsert for [00Pc0000001KBIu] Attachment trigger event AfterInsert for [00Pc0000001KBIv] Attachment trigger event AfterInsert for [00Pc0000001KBIw] Attachment trigger event AfterInsert for [00Pc0000001KBIx] Attachment trigger event AfterInsert for [00Pc0000001KBIy]: []: Trigger.CopyAttachmentstoOpps: line 16, column 1

trigger CopyAttachmentstoOpps on Attachment (after insert) {

    List<Attachment> insertAttList = new List<Attachment>();
    
    for (Attachment att:Trigger.new) {           
        String parentIdString = String.valueof(att.parentId);
        Service_Order__c oppId = [SELECT Opportunity__c FROM Service_Order__c WHERE ID = :att.parentId];
        system.debug('The ID is' + parentIdString);
        
        Attachment b = att.clone();
        b.parentid = parentIdString;
        insertAttList.add(b);     
        
    }
    if(insertAttList.size()>0)
    insert insertAttList;
    
}

AshwaniAshwani
Imran, This design is wrong. You cannot insert the object whom trigger is being fired this will cause recursion which lead to exception finally.
 You should first consider to fire trigger on custom object or opportunity to prevent recusion.

Alternatively, you can use @future call in trigger like:


trigger CopyAttachmentstoOpps on Attachment (after insert) {

    List<Attachment> insertAttList = new List<Attachment>();
    Set<String>  parentIDs = new Set<Ids>();
    for (Attachment att:Trigger.new) {          
        parentIDs.add(att.parentId);
    }

    @future
    public static void copyAttachments(List<String> ids)
    {
        String parentIdString = String.valueof(att.parentId);
        List<Service_Order__c> oppId = [SELECT Opportunity__c FROM Service_Order__c WHERE ID in:ids];
         // other stuff
    }
}

Hope this helps

imran Rahmanimran Rahman
@Avilion: What hook would you suggest I use instead of Attachments? the reason I have chosen to go down this route is because when uploading files to a Custom Object record the record itself is not updated becuase the files are stored in a child object.