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
hemmhemm 

In a trigger, telling the difference between a new Chatter File and a Link to an Existing one

I like using Chatter Files for Attachments because they are prettier and searchable. The downside is that, when you add one to an Opporutunity, you don't see it as an Attachment on the Account like an old-skool Attachment would.

 

So I am working on a trigger where, if a user posts a Chatter File to an Opportunity, it also links it up to the Account. Sample code is below. I am successfully checking for it being a Chatter File (ContentPost) and only running when linked to an Opportunity, but the trigger runs whether I am adding a new file and also if I am associating an existing file to an Opportunity.

 

Any ideas how to determine whether the ContentPost is using a new Chatter File or an Existing one?

 

for (FeedItem f: fis){

    String parentIdString = String.valueOf(f.parentId);

    if (parentIdString.startsWith(Opportunity.sObjectType.getDescribe().getKeyPrefix()) && f.type == 'ContentPost'){
        Opportunity o = [select id, accountId from Opportunity where id = :f.ParentId limit 1][0];
        if(o.AccountId != null){
            FeedItem newFI = new FeedItem();
                newFI.Type = 'ContentPost';
                newFI.RelatedRecordId = f.RelatedRecordId;
                newFI.ParentId = o.AccountId;
                newFI.title = f.title;
            insert newFI;
        }

    }

}

 NOTE: I am already aware of the issue with querying and DML in the loop. I will fix that later. I am mostly concerned with getting the scenario correctly handled.

 

 

hemmhemm

From my analysis, the best I can think of is to compare the following 3 dates either to be the same or maybe within a practical range of each other (1 second?):

 

- Create Date on FeedItem
- Create Date on ContentVersion (obtained by querying it using FeedItem.RelatedRecordId)
- Create Date on ContentDocument (obtained by querying it using ContentVersion.ContentDocumentId)

 

Thoughts? Wish there was a flag I could check, but this seems to be the only way I can find.

Jia HuJia Hu

This is an interesting question.

 

In your case, compare the CreatedDate of FeedItem and ContentVersion should be OK.

Based on my test, if you post a new file, the difference of CreatedDate of FeedItem and ContentVersion is in only 1-2 seconds.

May be different based on the file size.

 

But you still can't use this method now.

Since, you can't query ContentVersion to get the file posted on the Object.

This is a bug, and Salesforce promise to revise it in the next version.

Rajesh_ShahRajesh_Shah

The last statement is something that has stuck odd and I wanted to understand it better.

 

We have VF page that allows a user to upload an attachment that we later convert to FeedItem. THe VF Page also allows updating the FeedItem by inserting a new ContentVersion. The whole thing works fine in our dev org. We have created a package out of it and installed it to another org.

 

The same thing however doesn't works now. Instead of updating the FeedItem, the code is creating a new Feed. We have also found that query to ContentVersion based is not returning any values.


Can you give an idea what might be causing this issue?