You need to sign in to do that
Don't have an account?
Adnan 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;
}
}
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;
}
}
Shashank (Salesforce Developers)
Please go through this for help: http://salesforce.stackexchange.com/questions/76015/trigger-on-contentdocument-not-working
Anthony Blanc
You just need to create a ContentVersion and the ContentDocument will be automatically created.