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
Adnan PAdnan P 

DML operation INSERT not allowed on ContentDocument

Hello,

I'm trying to write a method that will allow me to clone Attachments (Type = File) when a Campaign is auto-created from a custom object.  I have a trigger that auto-creates a Campaign when Status = Approved on a record for the custom object called Internal_Request__c.  I would like to copy all Attachments forom the parent record and attach them to the newly created Campaign.

I was able to get the method to work when Type = Attachment for the Attachment on the parent record.  But when I try to clone and insert Attachments where Type = File I get the error "DML operation INSERT not allowed on ContentDocument".  Does anyone know if this is possible any other way?  Any feedback would be greatly appreciated.  I'm a beginner developer so I apologize if any of the below is not clear.

// ***This method works without any issues***
private void copyAttachmentsFromIr(List<Campaign> newList) {
    Map<Id, Id> idMap = new Map<Id, Id>();
    for(Campaign c : newList) {
      idMap.put(c.Internal_Request__c, c.Id);
    }
    Set<Id> irIdSet = idMap.keySet();
    List<Attachment> iRAttachmentList = (List<Attachment>)Database.query(SelectStarService.createSelectStarSoql('Attachment') + ' WHERE ParentId = :irIdSet');
    List<Attachment> campaignAttachmentList = new List<Attachment>();
    for(Attachment a : iRAttachmentList) {
      Attachment clonedAttachment = a.clone(false, true, false, false);
      clonedAttachment.ParentId = idMap.get(a.ParentId);
      campaignAttachmentList.add(clonedAttachment);
    }
    if (!campaignAttachmentList.isEmpty()) {
      insert campaignAttachmentList;
    }
  }


// ***I get the "DML operation INSERT not allowed on ContentDocument" when trying to insert***
  private void copyFilesIr(List<Campaign> newList) {
    Map<Id, Id> idMap = new Map<Id, Id>();
    for(Campaign c : newList) {
      idMap.put(c.Internal_Request__c, c.Id);
    }
    Set<Id> irIdSet = idMap.keySet();
    List<ContentDocument> iRFileList = (List<ContentDocument>)Database.query(SelectStarService.createSelectStarSoql('ContentDocument') + ' WHERE ParentId = :irIdSet');
    List<ContentDocument> campaignFileList = new List<ContentDocument>();
    for(ContentDocument a : iRFileList) {
      ContentDocument clonedFile = a.clone(false, true, false, false);
      clonedFile.ParentId = idMap.get(a.ParentId);
      campaignFileList.add(clonedFile);
    }
    if (!campaignFileList.isEmpty()) {
      insert campaignFileList;
    }
  }
ShashankShashank (Salesforce Developers) 
Please go through this for help: http://salesforce.stackexchange.com/questions/76015/trigger-on-contentdocument-not-working
Anthony  BlancAnthony Blanc
You just need to create a ContentVersion and the ContentDocument will be automatically created.