4
@RemoteAction
public static void DeletePriorties(string Rid)    {   
    ContentVersion sList =[Select id,Title,FileType,GeneratedDate__c,FilelinkedCode__c,ContentUrl,createdby.name
                            FROM ContentVersion 
                            WHERE SourceRequisitionId__c=:Rid];

    ContentVersion file= new ContentVersion();
    file.OwnerId=UserInfo.getUserId();
    file.FileLinkedCode__c=sList.FileLinkedCode__c;
    file.SourceRequisitionId__c=sList.SourceRequisitionId__c;
    file.pathOnClient=sList.pathOnClient;
    file.versionData=sList.versionData;
    file.GeneratedDate__c=sList.GeneratedDate__c;
    insert file;
    delete sList;
}

But I got this error:

 Compile Error: DML operation DELETE not allowed on ContentVersion at line 535 column 9 

I followed this link but i Don't KNOW HOW TO USE THIS QUERY. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm#sforce_api_objects_contentversion

  • 1
    You have to delete the ContentDocument and all its versions. – Adrian Larson Aug 16 '16 at 12:06
11

In the SFDC Objects documentation you referenced, at the top of the page is this line:

create(), describeLayout(), describeSObjects(), query(), 
retrieve(), search(), update(), upsert()

No delete - hence a DML delete on this object is not permitted.

This makes sense because in the user interface, you can't delete a version either, only add a new one

All ContentVersion versions are hung under (as a child) a ContentDocument. The ContentDocument, if deleted, deletes all child ContentVersions

From the SFDC Object reference documentation on ContentDocument:

When you delete a document, all versions of that document are deleted, including ratings, comments, and tags.

  • Note: delete() and undelete() features have been provided now. Please check same documentation link – Kamruzzaman Apr 9 '18 at 9:03
  • @Robi -- The doc link you posted is to ContentDocument, not ContentVersion which was the subject of the OP. One could always delete ContentDocument (that in turn, deleted all ContentVersion) – cropredy Apr 9 '18 at 13:41
  • Yeah. You still can not delete individual version of a content. This idea is under consideration success.salesforce.com/ideaView?id=08730000000BqAPAA0 – Kamruzzaman Apr 10 '18 at 8:52
0

First, find the ContentDocumentIds for the versions you need to delete (when you upload a file as a ContentVersion, contentDocument record is automatically created for you). Then, all you have to do is delete the contentDocuments with those Ids. Your SOQL query is also potentially assigning a list of records to a variable representing a single ContentVersion record, so that should be fixed unless you set this field as unique.

Your code then becomes:

@RemoteAction
public static void DeletePriorties(string Rid)    {
  List<ContentDocument> listToDelete = new List<ContentDocument>();
  List<ContentVersion> listToInsert = new List<ContentVersion>();
  for (ContentVersion conver : [
      SELECT
        id, Title, FileType, GeneratedDate__c, FilelinkedCode__c,
        ContentUrl, createdby.name, ContentDocumentId
      FROM ContentVersion
      WHERE SourceRequisitionId__c=:Rid
  ]) {
    listToDelete.add(new ContentDocument(
      Id = conver.ContentDocumentId,
    ));
    listToInsert.add(new ContentVersion(
      OwnerId = UserInfo.getUserId(),
      FileLinkedCode__c = conver.FileLinkedCode__c,
      SourceRequisitionId__c = Rid,
      pathOnClient = conver.pathOnClient,
      versionData = conver.versionData,
      GeneratedDate__c = conver.GeneratedDate__c
    ));
  }
  delete listToDelete;
  insert listToInsert;
}

You will probably want to add some error checking and a database savepoint in case your DMLs fail.

You can also adjust this method to accept a list of Rid strings to avoid any loops with SOQL inside:

@RemoteAction
public static void DeletePriorties(List<String> rids)    {
  List<ContentDocument> listToDelete = new List<ContentDocument>();
  List<ContentVersion> listToInsert = new List<ContentVersion>();
  for (ContentVersion conver : [
      SELECT
        id, Title, FileType, GeneratedDate__c, FilelinkedCode__c,
        ContentUrl, createdby.name, ContentDocumentId
      FROM ContentVersion
      WHERE SourceRequisitionId__c IN :rids
  ]) {
    listToDelete.add(new ContentDocument(
      Id = conver.ContentDocumentId,
    ));
    listToInsert.add(new ContentVersion(
      OwnerId = UserInfo.getUserId(),
      FileLinkedCode__c = conver.FileLinkedCode__c,
      SourceRequisitionId__c = conver.SourceRequisitionId__c,
      pathOnClient = conver.pathOnClient,
      versionData = conver.versionData,
      GeneratedDate__c = conver.GeneratedDate__c
    ));
  }
  delete listToDelete;
  insert listToInsert;
}

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.