You need to sign in to do that
Don't have an account?
Zander Zumbrunnen
Query for Text Attachment
Hi all,
I do note quite understand how to query for an attachment.
I have uploaded a .txt file to a custom object and am trying to query for it through apex.
However, everytime I run this code, it gives me this error.
"System.QueryException: List has no rows for assignment to SObject"
I want to be able to read this text document but google has been no help so far.
How do you read a text attachment from a query?
I do note quite understand how to query for an attachment.
I have uploaded a .txt file to a custom object and am trying to query for it through apex.
Attachment file = [SELECT Id, Body FROM Attachment WHERE ParentId = :recordId Limit 1];
However, everytime I run this code, it gives me this error.
"System.QueryException: List has no rows for assignment to SObject"
I want to be able to read this text document but google has been no help so far.
How do you read a text attachment from a query?
Check this :
Attachments uploaded in lightning aren't the same as attachments uploaded through salesforce classic
https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience
Thanks!
All Answers
Please use below code:
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Check this :
Attachments uploaded in lightning aren't the same as attachments uploaded through salesforce classic
https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience
Thanks!
Hi,
You can take references from the below
https://salesforce.stackexchange.com/questions/179452/query-customobject-with-attachment
Please mark it as the Best Answer so that other people would take references from it.
Thank You
Thank you for the quick replies.
Mukesh, that looks pretty close to what I have, I know that the recordId is not null so I don't think that changes much.
Abhinav, thank you for the link, I was able to query the file but I am not able to get the body, I do not think the ContentDocument object has that property unless it is called something else. The goal is to read a text file and ingest the data. Now that I have the ContentDocument Id, do I have to query for the attachment that relates to it? I am still a bit confused.
Suraj, thank you for the link as well, I tried to query for the main record and the child attachments, but when I debug the attachments there is nothing.
Here are the additional things I have tried:
CustomObject__c record = [SELECT Name, (SELECT Id FROM Attachments) FROM CustomObject__c WHERE Id = :recordId];
system.debug(record.Attachments);
(This would not have worked anyways because I got an error when trying to add the body from the attachment)
List<ContentDocumentLink> cdl = new List<ContentDocumentLink>([SELECT ContentDocumentId, ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId = :recordId Limit 1]);
system.debug(cdl);
(This one worked, but again, how do I access the body from this point?)
List<Attachment> file = new List<Attachment>();
file = [SELECT Id FROM Attachment WHERE ParentId = :recordId];
system.debug(file);
(I still get no results for some reason)
Debug Logs:
DEBUG|()
DEBUG|(ContentDocumentLink:{ContentDocumentId=*, Id=*})
DEBUG|()
I see a lot of the stack overflow questions are a few years old which is probably a lot of the methods no longer work, I am assuming that the ContectDocument objects are the way to go but I can't seem to figure out how to have the function like the Attachment object.
Any additional information would be very helpful.
Thank you.
I found out that the file contents are in the ContentVersion object.
Here is the code that works incase any one else has this issue: Thanks again for the help!