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
Behzad Bahadori 18Behzad Bahadori 18 

I need to unit test a Document with Document ID

I have a list and list should Contain a certain document that has an ID 


    List<String> blst = new List<String>();
    

      System.debug('inside the empty folder to set up the proposal');
                c = [SELECT Id, Name, Body
                         from Document
                        Where Id =: FoId];  
                        System.debug('Adding one file to the PDF ');
                blst.add(EncodingUtil.base64Encode(c.Body)); 
        

my unit test its keep telling me the id is empty even tho I make sure FoID is assign

Unit test FoId= '123456';

how can I test that 
Best Answer chosen by Behzad Bahadori 18
Deepak GulianDeepak Gulian
List<Document> docs = new List<Document>();
        for(Integer i=0;i<3;i++) {
             Document doc = new Document( 
                   Body = Blob.valueOf('My Document '+ i +' Text'), 
                   ContentType = 'application/pdf', 
                   DeveloperName = 'test_document', 
                   IsPublic = true, Name = 'My Document'+i,
                   FolderId = [select id from folder where name = 'My Test Docs'].id
                   )
                   docs.add(doc);
       }
        insert docs;
Sorry to forgot to add that folderID, now its here
 

All Answers

Deepak GulianDeepak Gulian
Unit test FoId = '123456';
c = [SELECT Id, Name, Body from Document Where Id =: FoId];  //Here it is wrong as per my understanding salesforce have 15 digit or 18 digit Id
You cannot assign any random integer value and query that.
JethaJetha
Hi Behzad,

This FoId should be a valid salesforce Id, Either 15 digit or 18 digit, both works fine.
Behzad Bahadori 18Behzad Bahadori 18
@deepak and @Jetharam well that was just an exmaple number the FoID if you really wanna know is this , but for some reason  its keep skipping it 015210000008w9j  : error indicates System.QueryException: List has no rows for assignment to SObject 
Deepak GulianDeepak Gulian
@isTest(SeeAllData=true)
Set this at the top of your test class.
Behzad Bahadori 18Behzad Bahadori 18
I have that as well  here is the other section that is not being covered and does error right at DocumentD 
for(String doc :selectedDocuments){
          if (doc != NULL && doc != ''){
            Document d = [SELECT Id, Name, Body
                         from Document
                        Where Id = :doc
                        ];
                    
            blst.add(EncodingUtil.base64Encode(d.Body));
            }
            
        }
Behzad Bahadori 18Behzad Bahadori 18
thanks for the respond it says REQUIRED_FIELD_MISSING, Required fields are missing: [FolderId]: [FolderId] when I want to insert docs on line 13
Deepak GulianDeepak Gulian
List<Document> docs = new List<Document>();
        for(Integer i=0;i<3;i++) {
             Document doc = new Document( 
                   Body = Blob.valueOf('My Document '+ i +' Text'), 
                   ContentType = 'application/pdf', 
                   DeveloperName = 'test_document', 
                   IsPublic = true, Name = 'My Document'+i,
                   FolderId = [select id from folder where name = 'My Test Docs'].id
                   )
                   docs.add(doc);
       }
        insert docs;
Sorry to forgot to add that folderID, now its here
 
This was selected as the best answer
Behzad Bahadori 18Behzad Bahadori 18
Tank you for respond again, still is complaining can i use this FolderId = UserInfo.getUserId() ? 
Behzad Bahadori 18Behzad Bahadori 18
it says Line 08 System.QueryException: List has no rows for assignment to SObject as if Folder ID doesnt exist
Deepak GulianDeepak Gulian
You need to use some existing folder name instead of 'My Test Docs'
Behzad Bahadori 18Behzad Bahadori 18
Now its complaining 
System.QueryException: List has no rows for assignment to SObject