+ Start a Discussion
MIKE_DAYMIKE_DAY 

Trigger to insert Chatter File on a Task to the Attachment Related List

Hi Guys,

 

I have started using Chatter for Mobile and, in many cases, this allows me to attach images to records and have them appear as attachements against the record.  This is not supported however when uploading Chatter files to Tasks as the image will stay in the chatter feed but not the Attachment Related List.

 

Does anyone have an example of a trigger that will take a Task's Chatter feed (files only) when created and insert it as a Task Attachment?

 

If not, could you point me in the right direction please?

 

Many thanks

 

Mike

Best Answer chosen by Admin (Salesforce Developers) 
MIKE_DAYMIKE_DAY

I have a solution that works for me.  

 

trigger Feedattachment on FeedItem (after insert) {
try { // Start of Try
List<Attachment> Att = new List<Attachment>();
    for (FeedItem f : trigger.new) 
    {  // Start of For Loop
      string what = f.ParentId;
       if((what.startsWith('00T')) && (f.Type == 'ContentPost')){ 
                  try {
                     System.debug('triggerfire');
                       Att.add(new Attachment(
                       ParentId = f.ParentId,
                       Name = f.ContentFileName + ' ' + System.now(),
                       body = f.ContentData
                       ));                                                           
                    } catch (Exception e) { }               
                   } // End If            
   } // End Loop
  insert Att;
 } // end try
  catch (Exception e) {
 } // end catch
} // End Trigger

All Answers

MIKE_DAYMIKE_DAY

I have a solution that works for me.  

 

trigger Feedattachment on FeedItem (after insert) {
try { // Start of Try
List<Attachment> Att = new List<Attachment>();
    for (FeedItem f : trigger.new) 
    {  // Start of For Loop
      string what = f.ParentId;
       if((what.startsWith('00T')) && (f.Type == 'ContentPost')){ 
                  try {
                     System.debug('triggerfire');
                       Att.add(new Attachment(
                       ParentId = f.ParentId,
                       Name = f.ContentFileName + ' ' + System.now(),
                       body = f.ContentData
                       ));                                                           
                    } catch (Exception e) { }               
                   } // End If            
   } // End Loop
  insert Att;
 } // end try
  catch (Exception e) {
 } // end catch
} // End Trigger

This was selected as the best answer
Dima GoncharovDima Goncharov
Thank you for posting the code. However, FeedItem.ContentType (which holds the body of the attachment) is deprecated as of API Version 36. We are on API Version 38 now. Is there a way to copy the attachment from the feed to another object as an attachment so it shows up in the related lists? Thanks!