4

So we have an object to which some images are attached via the Chatter feed.

Imagine the Id of the FeedItem is 0D5b000000L3ZEE.

Now I want to have a formula field that uses the IMAGE() function to display that attached image. How would I do that?

I imagine using an Apex trigger that creates the URL at which the image can be viewed. And then formula field looks up that URL and displays the image. But what is the correct URL that I can use?

  • 1
    Have you tried URLFOR and $Action Download permutations? – bigassforce Feb 7 '14 at 15:53
  • No... How would that work? If you could post it as an answer I would be very grateful – Willem Mulder Feb 7 '14 at 16:05
5
+50

The FeedItem has a field called RelatedRecordId which is defined as:

ID of the ContentVersion object associated with a ContentPost. This field is null for all posts except ContentPost. For example, set this field to an existing ContentVersion and post it to a feed as a FeedItem object of TypeContentPost.

If you right click on the download link for the chatter image you get an URL for the download.

It will be something like: https://c.na15.content.force.com/sfc/servlet.shepherd/version/download/068i0000002EqDEBQ5?asPdf=false&operationContext=CHATTER

The ID portion of the URL, 068i0000002EqDEBQ5, is the ContentVersion ID, which is the same as the RelatedRecordId on the FeedItem.

Putting it all together...

Create a custom text field on your object called Content_Version_ID__c and capture the FeedItem’s RelatedRecordId in it (code below). Construct an IMAGE formula field with the following formula:

IMAGE("/sfc/servlet.shepherd/version/download/“ & Content_Version_ID__C, "Chatter Image")

Here is the FeedItem trigger code to copy the RelatedRecordId to the custom Content_Version_ID__c field. I wrote it to work for Leads, but you can modify it for whatever Object you need.

trigger FeedItemTrigger on FeedItem (after insert) {
    Map<Id, Id> possibleRelateds = new Map<Id, Id>();
    for (FeedItem fi : Trigger.New) {
        if (fi.RelatedRecordId != null) {
            possibleRelateds.put(fi.RelatedRecordId, fi.ParentId);  
        }
    }

    Map<Id, Lead> leads = new Map<Id, Lead>();
    for (FeedItem fi : [
         Select Id, RelatedRecordId, ParentId
         From FeedItem
         Where Parent.Type = 'Lead' 
               And ParentId In :possibleRelateds.values()
    ]) {
        if (fi.RelatedRecordId != null &&
            possibleRelateds.get(fi.RelatedRecordId) == fi.ParentId &&
            !leads.keySet().contains(fi.ParentId)
        ) {
            Lead leadToUpdate = new Lead( 
                Id = fi.ParentId, 
                Content_Version_ID__c = fi.RelatedRecordId
            );
            leads.put(leadToUpdate.Id, leadToUpdate);
        }
    }
    update leads.values();
}

As noted, this will not update to point to a newer version of the file if a newer version is uploaded.

  • Great, that works! I was doing the same thing, only used the FeedItem.Id instead of the FeedItem.RelatedRecordId, which, now obviously, does not work: a FeedItem is not the same as the File that it contains. I have one question however: 1) what if somebody uploads a new version of the file (click on file > upload new version)? The field would still show the old image, right? – Willem Mulder Feb 10 '14 at 9:44
  • Oh, and a tip: if you want to use a thumbnail instead of the full file, use this URL prefix: /sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB120BY90&versionId= – Willem Mulder Feb 10 '14 at 9:46
  • That's right. It would still show the old image. Perhaps, you could do something with a ContentVersion object trigger. – Peter Knolle Feb 10 '14 at 10:38

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.