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
Joey HoJoey Ho 

Insert a copy of Attachment into a Chatter Feed as a File for Sharing

Hi All, 

 

There are plenty of code online that looks at inserting Attachments into Salesforce objects however I have a requirement to do something a little different to this.

 

Whenever a file is manually uploaded onto a Case object as an Attachment on the Case record, I would like to have a trigger that inserts the same Attachment into the Chatter Feed as a File on the same Case record. Although I can write the trigger of picking up the Attachments fine, I do not know where to start in APEX if I wanted to insert this Chatter Feed File using the trigger.

 

Could someone please direct me on what standared objects and calls I should be making to insert this Feed File?

 

Many thanks!

bob_buzzardbob_buzzard

You'd create a FeedItem and set the content fields to the attachment body etc.  Here's a snippet of code from one of my dev orgs - its not a trigger, but from a visualforce controller, but should be easy enough to adapt.

 

	public void addAttachment()
	{
		attachment.parentId=caseId;
		insert attachment;
		
		//Adding a Content post
		FeedItem post = new FeedItem();
		post.ParentId = caseId; //eg. Opportunity id, custom object id..
		post.Body = 'Attachment added';
		post.ContentData = attachment.body;
		post.ContentFileName = attachment.Name;
		insert post;
	}

 

Joey HoJoey Ho

Thanks Bob. I got the trigger to add Attachments to the case record Feed and thought I post it for others to work off as well.

 

Another question though, the reason why I am doing this copy Attachment in the casefeed is because I am trying to take advantage of the 'share via link' (the public link to the file in Content) however according to your response in this post you mentioned that its not possible to get this public link via APEX calls. Obviously I would like to use this link to send our email alerts which include this public link so that the case Teams can access the attachment publically.

 

Is there another way of doing this or does the Spring 13' release have any new improvements to allow us to do this?

 

Many thanks for your advise!

 

trigger AttachFileToCaseFeed on Attachment (before insert) {	
	ID caseId;	
	list<FeedItem> listOfFeedFiles = new List<FeedItem>();	
	
	if(Trigger.isBefore){
	
		for(Attachment attachment : trigger.new){
		   string checkIfCase = string.valueof(attachment.ParentId);
		 
		   if(checkIfCase.startsWith('500')){ 
				//Adding a Content post
				caseId = attachment.ParentId;
				FeedItem post = new FeedItem();
				post.ParentId = caseId; //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;	
	}	
	
}

 

bob_buzzardbob_buzzard

There's nothing that I've spotted in the winter 13 release notes that indicates this is supported I'm afraid.

MG1MG1

Bob - We have similar code working for attaching documents to a feed and they appear in the feed as expected. Do you know which object you can find details about the Chatter Feed file attahcment like the file name so we can verify attachment in tests?

Carlos NaranjoCarlos Naranjo
I'm using this code but I see that after adding an attachment in the Notes & Attachment related list I get two files after inserting one attachment. One is Type Attachment and the other Feed Accthament, there is a way to only have one file in the related list or this is something that needs to be just like that?
Joey HoJoey Ho
Hi Carlos, 
I think that is a defaul for adding feed files. You may just need to create and after insert trigger that deletes the original Attachement (the 1st one) leaving you with the Feed File attachment in notes and attachments section.
Carlos NaranjoCarlos Naranjo
Hi Joey, 

That is a good idea but I think that having  a feed version in the notes and attachments related list is not bad after all. If a user deletes the feed attachment type the system will keep the attachment type file, which make sense. So I will keep it like this for now. Thanks for you reply. 
Samrat ChakrabortySamrat Chakraborty
Hi,
What if I want to add files directly to chatter without saving it in "Notes and Attachment". The reason being the PDF that I am generating in VFpage is more than the size limit of Attachment but less than that of FIles. Please let me know, if you guys can give me an idea around this??