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
CuriousJohnCuriousJohn 

Accessing (Unstructured) Document Contents in Apex

The standard technique for accessing Document Tab files in Apex is not working for me.

 

Here's the code I'm using:

 

List<Document> docs;
docs = [select id, name, body from Document 
       where name = 'Donations_Test_Data']; 
 

 

Donations_Test_Data is the "Document Unique Name" (i.e. API name) of a CSV file stored in the Documents Tab. This SOQL query produces zero (0) records of output.

 

The log looks like this:

 

07:41:45.283 (283369000)|SOQL_EXECUTE_BEGIN|[372]|Aggregations:0|select id, name from Document where name = 'Donations_Test_Data_XXX'
07:41:45.292 (292463000)|SOQL_EXECUTE_END|[372]|Rows:0

 

I need to be able to access this test data programmatically in order to get test coverage.

Best Answer chosen by Admin (Salesforce Developers) 
CuriousJohnCuriousJohn

O.k., I found the problem. It has NOTHING to do with taking data from files in the Document Tab, per se, so sorry to have misled you, Vinit Kumar.

 

I'm trying to run this code from a test method, and test methods can only fetch data that the test method inserted. Test methods may not fetch regular data from the database!!!!

 

You can override this behavior with the SeeAllData flag like this:

 

@isTest(SeeAllData=true) static void testDocRead() {

     [YOUR TEST CODE]
}

 

Relevant doc page: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm 

 

Or alternatively, you can apply the SeeAllData annotation to an entire test routine in this way:

 

@isTest(SeeAllData=true)
private class MyTestClass {
   [YOUR TEST CODE]
}

 

Here's an article on the isolation of Test Data from Organizational Data&colon;

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm 

 

Vinit Kumar's answer was still very helpful.

 

If anyone knows where I can find further documentation on the Document and Page objects (see my three concerns in the 2nd post above), I would be grateful.

 

Thanks.

 

 

    

All Answers

CuriousJohnCuriousJohn

BTW, I am unable to find any of the following bits of information in the Salesforce docs:

(1) Guidance with respect to accessing documents in folders. Do you need to provide folder information in the SOQL query?

(2) Guidance concerning the SOQL syntax required to access the BODY of a document within Apex. I am assuming that a query like [select id, name, body ...] is required (with the 'body' field included), because according to a forum post I read, this is how you access the contents of a text file. But does the same technique work for files in the Doc Tab? Where is this explained in Salesforce documentation?

(3) Guidance, generally speaking, concerning the Document class / object, it's methods, properties, examples of its use

 

There are several "mystery" classes like the Document class for which there appears to be no information in the Salesforce docs. Another example would be the Page class. There's a doc page for PageReference, but not for Page. I assume, therefore, that both Document and Page are classes that are familiar to Java programmers and so the Saleforce doc team decided to economize on their efforts by leaving this unexplained. If so, then for the benefit of those of us who are not Java programmers, I wish Salesforce would enhance their docs by adding links to Java material in appropriate places. 

Vinit_KumarVinit_Kumar

change the code from

 

List<Document> docs;
docs = [select id, name, body from Document
where name = 'Donations_Test_Data'];

 

to 

 

List<Document> docs;
docs = [select id, name, body from Document
where name = 'Donations Test Data'];

 

i.e. you don't need API name in the where filter ,you should be using Document name

CuriousJohnCuriousJohn

Interesting. O.k., I did certainly need to know that. Thanks!

 

However, I made the change you suggested, and I'm still getting zero records of output.

 

Question: I just noticed that my document as a "Namespace Prefix" of "JohnsVersion1". Should the SOQL query make use of the Namespace prefix?

Vinit_KumarVinit_Kumar

That should not make difference here.Please make sure you have the access to the folders in which that document is stored.

CuriousJohnCuriousJohn

I first tried with the document in the "My Personal Documents" area. When that didn't work I tried putting the document in a public folder.

 

Document NameDonations Test Data
Document Unique NameDonations_Test_Data  
Namespace PrefixJohnsVersion1  
Internal Use OnlyNot Checked
Document Content SearchableChecked
FolderUK Data Migration Test Data
AuthorJohn StrongXXXXXXXX
File Extensioncsv
MIME Typeapplication/vnd.ms-excel
Size26KB
DescriptionTest data for UK donation data migration project
Keywords 
  
Vinit_KumarVinit_Kumar

Below is mine and I am able to query it :-

 

Document NameCSV Book
Document Unique NameCSV_Book  
Namespace PrefixVinit  
Internal Use OnlyNot Checked
Document Content SearchableNot Checked
FolderMy Personal Documents
AuthorVinit KumarVinit Kumar [Change]
File Extensioncsv
MIME Typeapplication/vnd.ms-excel
Size10 bytes
Description 
Keywords 
 View file

 

Please make sure that you are using name='Donations Test Data' in the where filter

CuriousJohnCuriousJohn

O.k., I found the problem. It has NOTHING to do with taking data from files in the Document Tab, per se, so sorry to have misled you, Vinit Kumar.

 

I'm trying to run this code from a test method, and test methods can only fetch data that the test method inserted. Test methods may not fetch regular data from the database!!!!

 

You can override this behavior with the SeeAllData flag like this:

 

@isTest(SeeAllData=true) static void testDocRead() {

     [YOUR TEST CODE]
}

 

Relevant doc page: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm 

 

Or alternatively, you can apply the SeeAllData annotation to an entire test routine in this way:

 

@isTest(SeeAllData=true)
private class MyTestClass {
   [YOUR TEST CODE]
}

 

Here's an article on the isolation of Test Data from Organizational Data&colon;

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm 

 

Vinit Kumar's answer was still very helpful.

 

If anyone knows where I can find further documentation on the Document and Page objects (see my three concerns in the 2nd post above), I would be grateful.

 

Thanks.

 

 

    

This was selected as the best answer
Vinit_KumarVinit_Kumar

I am aware of this functionality,if you have told me earlier that you are running the query in testmthod I could have informed you the root cause earlier.However,good you found out :)

pluviosillapluviosilla

I don't doubt that you could have, Vinit. As doctors know all to well, when it comes to the patient's idea about what is wrong with him, sometimes it is useful to the dotor, sometimes completely useless.  ;-)

 

You did give me a critical tip. I was confused about the use of the API name. Thanks very much for your help!