I will have users upload a .txt file in the Salesforce Files uploader (shared to a particular group).

Then, I want to access the text of that file within my Apex Class to do something with it.

I'm guessing I'd have to query it by name using SOQL, then access it's blob and change it to string. I'm just not sure how to do this even after reading the ContentDocument, ContentVersion and ContentBody Documentation.

Could someone suggest some code to put in my class?


  • *thanks for the downvote without reason :( * - anyways, seems like the field I am looking for is ContentData of a ContentVersion Object which holds a base64 blob. I will post the solution once I get it working. Cheers, – neildaemond Jan 26 '18 at 6:18
  • 3
    Hi neildaemond. I'm not the downvoter, but I'd like to take a moment to invite you to read How to Ask for what makes a good question here. At minimum, you'll want to show either some research notes of what you've found and/or the code you've written so far, preferably along with a Short, Self Contained, Correct, Example that you're stuck on. Of course, if you discover the answer yourself, please do feel free to add the answer in the area below (don't post it in your question; it'll just be deleted). We'll be glad to help you once you've shown a bit of effort. – sfdcfox Jan 26 '18 at 6:42
  • thanks @sfdcfox, it's my first post on here so the feedback is very helpful :) I'll try make future posts follow the guide better. – neildaemond Jan 26 '18 at 7:12

The main point of confusion is that the ContentDocument object doesn't have any field which contains the files content (blob data).

To get text from a user uploaded .txt file, I had to query for the VersionData field of the ContentVersion object. The VersionData field contains the blob data of the uploaded file as type Blob.

Also, every time a user uploads a new version of that file, another record of ContentVersion is added. Therefore, I've checked that IsLatest equals true to ensure that I get the most recent version of that file.

The resulting Apex Code:

String file_title = 'my_file_title';

List<ContentVersion> files = [SELECT Id, Title, IsLatest, VersionData FROM ContentVersion WHERE Title = :file_title AND IsLatest = true];

String files_txt = files[0].VersionData.toString();

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.