0

When you add an email to Salesforce using Salesforce for Outlook, the email is added as a new task. When you add email attachments using Salesforce for Outlook, attachments are included on the Attachments related lists (on Task and Account).

How can I write Apex to include the attachments on the Attachments related lists for Task and Opportunity instead (or remove attachment from Account and add to Opportunity)?

0

This answer makes the assumption you are selecting the opportunity when attaching an email to a record.

This also assumes they are inserted as Tasks and not events. Otherwise, youll have to type everything as events.

006 is the keyprefix for opportunity so..

Trigger triggerName on Task (after insert) {
    if (trigger.isAfter && trigger.isInsert) {
        List<Attachment> attachmentsToInsert = new List<Attachment>();
        List<Attachment> attachmentsToDelete = new List<Attachment>();
        for (Task t : Trigger.New) {
            String tWhatId = (String) t.whatId;

            if (tWhatId.left(3) == '006') { // Task is assigned to an opportunity
                for (Attachment a : t.attachments) {
                    temp att = a.clone();
                    att.parent = tWhatId;
                    attachmentsToInsert.add(att);
                    attachmentsToDelete.add(a);
                }
            }
        }

         if (attachmentsToInsert.size() > 0) {
             insert attachmentsToInsert;
         }

         // Minimize storage requirements by cleaning up attachments
         if (attachmentsToDelete.size() > 0) {
             delete attachmentsToDelete;
         }
    }
}

This should get you close.

One final Consideration:

Attachments may not exist at the exact moment the task is created (as the task has to exist to provide an id for the parent field on attachment. You may have to create the trigger at the attachment level and run a similar check to what I provided to see if the parent is a task. If the parent is a task, check the task's whatId to see if its an opp, and if it is an opp, clone the attachments.

Update:

Here is a question I asked a while back that is similar to what you're trying to do. This might provide some extra information on the cloning aspect of moving attachments from one object to another.

|improve this answer|||||
  • Toby, (1) much better practice ... if (t.WhatId.getSobjectType() == Opportunity.SObjectType)... and you can get rid of the string casting ; (2) test for empty list before insertion is unnecessary as no DML limits burned on empty list; same for deletes. – cropredy Mar 20 '17 at 6:01
  • OP also said tasks are attached to accounts and need to be moved to Oppos – cropredy Mar 20 '17 at 6:07
  • I tried this and it says "Compile Error: Invalid type: temp at line 10 column 21" – SSt Jun 15 '17 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.