Have you ever wanted to filter the Attachments associated with a Salesforce record? While Salesforce provides a standard page for uploading Attachments and Notes, there is no way to gain access to the file before it is attached, and there is no way to customize the filename once it has been attached.
Creating a custom Visualforce page to override the standard "Add Attachment" page seems like overkill for the simple task at hand. But what about using Visualforce to attach a file through Chatter and assigning it a Topic that we can use for searching and filtering? Perfect!
All of the code below is available for download from github. First, a few notes:
Huge shout out to Ryan Scott for his blog post Embedding Chatter in Custom Visualforce Pages; I started with his idea and augmented it for this specific solution.
First, we need a Visualforce element to render the Chatter Feed. This page is based on the Page from Ryan Scott's post:
For my solution, the page uses standard Salesforce stylesheets and is launched by a button on my custom object's standard Edit page.
My modifications were:
apex:pageapex:pageMessages and an apex:form to support our controller.apex:actionFunction for assigning a Topic to the Chatter File once it's uploaded.apex:actionFunction defined on line 21 in the onComplete JavaScript event for the chatter:feed element.For simplicity's sake, I use controller methods to move between the pages rather than relying on external JavaScript.
This code only works for API version 36.0 or higher because the ContentApi.assignTopic method signature has changed.
chatter:feed element when the file is uploaded. The apex:actionFunction on the page is invoked. This will add a Topic to the most recently added FeedItem of type 'ContentPost'.Feed file to the same record at the same time, we may not get the correct one as we are filtering based on CreatedDate.My finished page looks like the one below; obviously, your page is yours to customize!
If you want to see a list of Chatter Files tagged with our Topic, then you'll want to create this page. The controller method described above demonstrates how to retrieve the Chatter Feed files attached to our object in Apex and the Visualforce page shows how to display them in a table as clickable links.
Used by the ListFeedFiles page to return only those Feed files that match our Topic. It can be used to further filter the results or to display details, including a link, to each Feed File.
The finished page looks like:
Be sure to make the following configurations in your org:
/apex/FeedFile?id={!YourObjectName.id}&topic='Your Topic Name'./apex/ListFeedFiles?id={!YourObjectName.id}&topic='Your Topic Name'. Don't forget to add this and the previous button to your page layout!Want more? See how we can help you leverage Salesforce and Chatter through our custom development services.