I have some Apex code written to create a new Account, and then create a new Library (ContentWorkspace) and new Folder (ContentFolder) within that Library to house assets for that Account. It appears that one of these Content objects is a "non-setup" object, so the following error is being thrown:
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): ContentFolder, original object: ContentWorkspace: 
Here is the code I have written to accomplish this:
// create Account Account acc = new Account(); acc.Name = 'Test'; insert acc; // create Library ContentWorkspace workspace = new ContentWorkspace(); workspace.Name = 'Test'; insert workspace; // create Folder within that Library workspace = [select Id from ContentWorkspace where Name =: 'Test']; ContentFolder parentFolder = [select Id from ContentFolder where Name =: workspace.Id]; ContentFolder folder = new ContentFolder(); folder.Name = 'Test'; folder.ParentContentFolderId = parentFolder.Id; insert folder;
Am I missing something? Or is this just not possible to sequentially do like this in Apex? The only "workaround" for this DmlException I found online was to make the DML operation for the "non-setup" object in a
@future method. This won't work because I need the Library to exist in order to properly parent the Folders, so if the Library creation is in a
@future method, the Folder creation code won't work since the Library doesn't exist yet.