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
adreameradreamer 

DML not allowed on Folder

Hi All,

 

I am trying to insert folders as part of testing methods and I I get the following compile error: "DML not allowed on Folder" at the insert point.

 

I see the Folder object access includes "creatable".

 

Any ideas would be much appreciated.

 

Thanks,

Fernando

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can not create folder in Apex Class, so you will not be able to create forlder in test class. You should do a SOQL and  you can use it. Please check size of folder before youtest further so that your test do no fail on the orgs where folder does not exists. Your test should be like, use this sort of condition in your test.

 List<Folder> lstFolder = [Select Id From Folder Where Name = 'YourFolderName' ];
                
                if(lstFolder.size() > 0)
                {
                    Document document = new Document(FolderId = lstFolder.get(0).Id, Name='Test Name',Keywords = 'Test');
                  }

 

All Answers

Shashikant SharmaShashikant Sharma

You can not create folder in Apex Class, so you will not be able to create forlder in test class. You should do a SOQL and  you can use it. Please check size of folder before youtest further so that your test do no fail on the orgs where folder does not exists. Your test should be like, use this sort of condition in your test.

 List<Folder> lstFolder = [Select Id From Folder Where Name = 'YourFolderName' ];
                
                if(lstFolder.size() > 0)
                {
                    Document document = new Document(FolderId = lstFolder.get(0).Id, Name='Test Name',Keywords = 'Test');
                  }

 

This was selected as the best answer
adreameradreamer

Thanks Shashikant !

JPClark3JPClark3

Doesn't seem to be a very good rule. Especially since they want you to create all the objects for unit tests.

 

http://success.salesforce.com/ideaView?id=08730000000IqJuAAK 

Kirill_YunussovKirill_Yunussov

Agreed.  Why do we have to query for it?  it goes against the best practices.