+ Start a Discussion
runnerbrayrunnerbray 

Add Attachment to Chatter Feed

I need to add account attachments to the chatter feed automatically. I have the following code which adds all attachments to the chatter feed, not just Account object attachments. How can I make it specific to the account object? Or make it specific to a file name?
 
trigger AttachFileToAccountFeed on Attachment (before insert) {   
    ID accountId; 
    list<FeedItem> listOfFeedFiles = new List<FeedItem>(); 
 
    if(Trigger.isBefore){
     
        for(Attachment attachment : trigger.new){
           string checkIfCase = string.valueof(attachment.Description);
          
           {
                //Adding a Content post
                accountId = attachment.ParentId;
                FeedItem post = new FeedItem();
                post.ParentId = accountId; //eg. Opportunity id, custom object id..
                post.Body = 'Attachment added';
                post.Type = 'ContentPost';
                post.ContentData = attachment.body;
                post.ContentFileName = attachment.Name;
                post.Title = attachment.Name;
                listOfFeedFiles.add(post);         
           }
        }
    }
     
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;
    }  
     
}

 
Best Answer chosen by runnerbray
ClintLeeClintLee
To restrict only to Account attachments you just need to ensure the ParentId is related to an Account.  

You can do the following as I did on line 9 below.
 
trigger AttachFileToAccountFeed on Attachment (before insert) {   
    ID accountId; 
    list<FeedItem> listOfFeedFiles = new List<FeedItem>(); 
 
    if(Trigger.isBefore){
     
        for(Attachment attachment : trigger.new) {
            // ensure the Id is an Account Id
            if(attachment.ParentId.getSObjectType() != Account.SObjectType)
                continue;

            string checkIfCase = string.valueof(attachment.Description);
          
            //Adding a Content post
            accountId = attachment.ParentId;
            FeedItem post = new FeedItem();
            post.ParentId = accountId;
            post.Body = 'Attachment added';
            post.Type = 'ContentPost';
            post.ContentData = attachment.body;
            post.ContentFileName = attachment.Name;
            post.Title = attachment.Name;
            listOfFeedFiles.add(post);         
        }
    }
     
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;
    }  
     
}

The Attachment's Name will be the name of the file, so if you want to check for a specific filename just do this:
 
if(attachment.Name == 'myFileName') {
  // do something or not
}

Hope that helps,

Clint

All Answers

ClintLeeClintLee
To restrict only to Account attachments you just need to ensure the ParentId is related to an Account.  

You can do the following as I did on line 9 below.
 
trigger AttachFileToAccountFeed on Attachment (before insert) {   
    ID accountId; 
    list<FeedItem> listOfFeedFiles = new List<FeedItem>(); 
 
    if(Trigger.isBefore){
     
        for(Attachment attachment : trigger.new) {
            // ensure the Id is an Account Id
            if(attachment.ParentId.getSObjectType() != Account.SObjectType)
                continue;

            string checkIfCase = string.valueof(attachment.Description);
          
            //Adding a Content post
            accountId = attachment.ParentId;
            FeedItem post = new FeedItem();
            post.ParentId = accountId;
            post.Body = 'Attachment added';
            post.Type = 'ContentPost';
            post.ContentData = attachment.body;
            post.ContentFileName = attachment.Name;
            post.Title = attachment.Name;
            listOfFeedFiles.add(post);         
        }
    }
     
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;
    }  
     
}

The Attachment's Name will be the name of the file, so if you want to check for a specific filename just do this:
 
if(attachment.Name == 'myFileName') {
  // do something or not
}

Hope that helps,

Clint
This was selected as the best answer
runnerbrayrunnerbray
Thanks Clint! It works great in sandbox, but when I go to deploy in productions I get the following error:

Code Coverage Failure
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. AttachFileToAccountFeed

This is the first trigger I've ever attempted. What do I need to do to resolve the code coverage error?
ClintLeeClintLee
You'll need a test class that covers your code and makes the proper assertions.  The one below should work for you.
 
@istest
public with sharing Test_AttachFileToAccountFeed
{
    /**
     *  Create an Account and an Attachment related to the Account.
     *  Ensure the Chatter Post was created.
     *
    **/
    static testmethod void testOne()
    {
        Test.startTest();
        
        Account acct = new Account(Name = 'Test Account');
        insert acct;

        // create an Attachment related to the above Account.
        // inserting the Attachment will cause the trigger to run.
        Attachment att = new Attachment(Name = 'Test Attachment', ParentId = acct.Id, Body = Blob.valueOf('the body'));
        insert att;

        Test.stopTest();

        // ensure the Chatter post was created
        Integer numPosts = [ select count() from FeedItem where ParentId = :acct.Id];
        System.assertEquals(1, numPosts);
    }

   /**
    *  Create a Lead and an Attachment that is related to the Lead.  
    *  Ensure that a Chatter Post was NOT created.
    *
   **/
   static testmethod void testTwo()
   {
        Test.startTest();
        
        Lead lead = new Lead( FirstName = 'Test', LastName = 'Lead', Company = 'NA', Status = 'Open');
        insert lead;

        // create an Attachment related to the above Lead.
        // inserting the Attachment will cause the trigger to run.
        Attachment att = new Attachment(Name = 'Test Attachment', ParentId = lead.Id, Body = Blob.valueOf('the body'));
        insert att;

        Test.stopTest();

        // ensure the Chatter post was NOT created since its parent is not an account.
        Integer numPosts = [ select count() from FeedItem where ParentId = :acct.Id];
        System.assertEquals(0, numPosts);
   }
}

Hope that helps,

Clint 
ClintLeeClintLee
On Line 48 in the test class I posted you'll need to change :acct.Id to :lead.Id.
runnerbrayrunnerbray
On line 2 I’m getting this error “unexpected token: 'Test_AttachFileToAccountFeed'”
ClintLeeClintLee
Add the word "class" after "with sharing".  Like this.
public with sharing class Test_AttachFileToAccountFeed

 
runnerbrayrunnerbray
Thanks for the help Clint! I added Class and removed test 2 from the test class. Code coverage is good. I've deployed to production and everything is working as tested.
Naresh AVNaresh AV
Hi,

I tried your code, but i'm facing this issue.
'Invalid field ContentFileName for SObject FeedItem'
Can you help me on this,
Thanks in advance