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
Hanna BergsmaHanna Bergsma 

Help with ContentDocument Ownership Trigger : DML Operation insert not allowed

I want to change the ownership of a file (ContentDocument) on insert.
When I wrote my test class im getting the error:DML operation Insert not allowed on ContentDocument. Trigger is below test class

@isTest(seeAllData=true)
private class Test_File_OwnershipTrigger
{
    static testMethod void TestContentDocument()
    { 
       ContentDocument testContentDocument = new ContentDocument();
       testContentDocument.Title ='Test Name';
       testContentDocument.ParentId = '02sf0000002WKN7AAO';
       insert testContentDocument;
    }
}


Trigger: 
trigger File_Ownership on ContentDocument (before insert) 
{
    for(ContentDocument CD: Trigger.New)
    {
        if(CD.ParentId != '')
        {
            CD.Ownerid='[my id is placed here]';
        }
    }
}
 
ShirishaShirisha (Salesforce Developers) 
Hi Hanna,

Greetings!

You might need to update the ContentVersion related to ContentDocument .I would suggest you to refer the below thread:

https://salesforce.stackexchange.com/questions/76015/trigger-on-contentdocument-not-working

https://salesforce.stackexchange.com/questions/98183/contentdocument-object

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Hanna BergsmaHanna Bergsma
Thanks,

I tried your suggestion with the Document to Version, but nothing happens when I update or insert a file now. I'm very new to this, and im not sure what I missed. Other blogs suggest using an After insert or update, but as I said I am too much of a beginner to do so on my own.

trigger FileOwnership on ContentVersion (before insert, before update) 
{
    for(ContentVersion CD: Trigger.New)
        {
            CD.OwnerId='005F0000002xxxxxxxx';
        }  
}