2

Please excuse any misstatements or blunders in my comments as I'm VERY new to Apex development. However, I've found code for a trigger that allows me to copy/clone attachments from a custom object (sales invoices) to a parent object (account). However, I've been told that it's best practice to split this into an Apex Class with a simple Trigger calling the Class. Herein is where my Apex development skills are severely lacking.

My existing Trigger code is (shamelessly re-used from here https://salesforce.stackexchange.com/a/44184/28717:

trigger CopyAttachmentsToAccount on Attachment (after insert) {

// collect a set of Sales Invoices 'parent' IDs from the attachments inserted
Set<Id> serviceInvoiceIds = new Set<Id>();
for(Attachment file : Trigger.new) {

    // only collect those that are for the c2g__codaInvoice__c object (others can be ignored)
    if(file.ParentId.getSObjectType() == c2g__codaInvoice__c.getSObjectType()) {
        serviceInvoiceIds.add(file.ParentId);
    }
}

if(!serviceInvoiceIds.isEmpty()) {

    // find the Account to which the c2g__codaInvoice__c relates
    Map<Id,c2g__codaInvoice__c> serviceInvoiceMap = new Map<Id,c2g__codaInvoice__c>([Select c2g__Account__c From c2g__codaInvoice__c Where Id IN :serviceInvoiceIds]);

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

    for (Attachment file : Trigger.new) {
        Attachment newFile = file.clone();
        newFile.ParentId = serviceInvoiceMap.get(file.ParentId).c2g__Account__c;
        attachments.add(newFile);
    }
    // finally, insert the cloned attachments
    insert attachments;
}
}

Any help that can be provided would be greatly appreciated!

2 Answers 2

2

Kudos for trying to code cleaner. I have not tried to optimize your trigger, but here is how to split it into a new class.

Your trigger would look like this:

trigger AttachmentTriggers on Attachment (after insert) {
   AttachmentTriggerHandler.copyAttachmentsToAccount(trigger.new);

}

And your class like that:

public class AttachmentTriggerHandler {
  public static void copyAttachmentsToAccount(List<Attachment> attachs) {
     // collect a set of Sales Invoices 'parent' IDs from the attachments inserted
     Set<Id> serviceInvoiceIds = new Set<Id>();
     for(Attachment file : attachs) {
       // only collect those that are for the c2g__codaInvoice__c object (others can be ignored)
       if(file.ParentId.getSObjectType() == c2g__codaInvoice__c.getSObjectType()) {
         serviceInvoiceIds.add(file.ParentId);
       }
     }

     if(!serviceInvoiceIds.isEmpty()) {
       // find the Account to which the c2g__codaInvoice__c relates
       Map<Id,c2g__codaInvoice__c> serviceInvoiceMap = new Map<Id,c2g__codaInvoice__c>([Select c2g__Account__c From c2g__codaInvoice__c Where Id IN :serviceInvoiceIds]);

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

       for (Attachment file : attachs) {
         Attachment newFile = file.clone();
         newFile.ParentId = serviceInvoiceMap.get(file.ParentId).c2g__Account__c;
         attachments.add(newFile);
       }

       // finally, insert the cloned attachments
       insert attachments;
     }
   }
}
2
  • 1
    Sebastian, thank you very much! It's almost scary how close I got in my attempts (missing a variable name). The only thing I had to change from your example above is to make it a "public static void" method.
    – M Shirrell
    Commented Feb 11, 2016 at 18:41
  • That one was a stupid one. :) Just fixed so whoever looks at this next gets the right answer. I am glad it worked! Don't forget to upvote! Commented Feb 11, 2016 at 18:47
0
  1. Find the generic parts in your Apex trigger
  2. Move all the common part to a class
  3. Make it a static method if you are trying to use it as utility (without creating an instance)

Here we don't require any information more than serviceInvoiceIds from this trigger

So lets write a class

Class MyClass {
  Public static void execute( Set<Id> serviceInvoiceIds) {
Do all the logic here 
  Starting from the line which says
if(!serviceInvoiceIds.isEmpty()) {
......
}
}
}

I am leaving the interesting part of naming things and implementation to you. Hope you got the idea.

You must log in to answer this question.

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