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
keady@sfdckeady@sfdc 

How to get a name of the uploaded file via chatter?

Hi,

 

I need to get the name (title) of the file uploaded by chatter. when i upload by selecting a file from salesforce i'm able to get the title from ContentVersion but when i upload a file from desktop, the RelatedRocordId in ContentVersion does not have any record. Can some one please help me to find out how to get the title of the file in this case?

 

I have gone through the API's i did not find any relationship betweeen ContentDocument and FeedItem. Where as there is a relationship between FeedItem and ContentVersion. But if the file not a content (uploaded from local machine) then there will not be any data present in the ContentVersion for the uploaded file. Please Advice.

 

below code works fine if the file is a content or uploaded a file from Salesforce via chatter. But it wont work if the file is uploaded from local.

 

Sample code:

Trigger on FeedItem

List<FeedItem> statusFeeds = trigger.new;

for(FeedItem fd : statusFeeds){

List<ContentVersion> cvList = [select id, ContentDocumentId, Title fromContentVersionwhere Id = :fd.RelatedRecordId limit 1];

Status__c = fd.Body;

Attachment_Link__c = System.Label.ServerURL + '/'+ cvList[0].ContentDocumentId;

File_Name__c = cvList[0].Title;

..........

........

.....

}

Jia HuJia Hu
with 'after insert' trigger, I can get the ContentVersion info. when upload from both Salesforce and local with your code.
keady@sfdckeady@sfdc

The query:

cvList = [select id, ContentDocumentId, Title fromContentVersionwhere Id = :fd.RelatedRecordId limit 1]; returning zero records when i upload a file from local... and if i check ContentVersion against the RelatedRecordId, ContentVersion does not retuen any rows.. am i missing something? is the Trigger.new is correct in after insert?

Jia HuJia Hu

trigger UploadFile on FeedItem (after insert) {

List<FeedItem> statusFeeds = trigger.new;
for(FeedItem fd : statusFeeds){
if(fd.Type == 'ContentPost') {
ContentVersion cv = [Select id, ContentDocumentId, Title from ContentVersion Where Id = :fd.RelatedRecordId ];
System.debug(' ----------- Feed Body: ' + fd.Body);
System.debug(' ----------- Document Id: : ' + cv.ContentDocumentId);
System.debug(' ----------- Document Title: : ' + cv.Title);
}
}

}

keady@sfdckeady@sfdc

I have the exact code which works fine for the files uploaded from sf, but same code returns zero records if the file is uploaded from local. :-( :-( is the query returning any record for you when you upload from local? any configuration has to be changed locally to make this work?

Jia HuJia Hu
I guess you are testing on the Object Feed.
This method works on UserFeed, Chatter Group Feed, but not for Feed on the Object, such as Account, Case,....