1

We need to get a solution / workaround to insert in ContentDocumentLink several links when a File that has just been inserted (ContentVersion) and sibling records of other Custom Object.

A third system will attack the table ContentVersion by inserting a PDF in Files and leaving it linked to a record of the object Custom Object. That record has a unique code that can be shared with other records of the object Custom Object being twinned and therefore, sharing the PDF inserted in Files that we want to leave linked for all records with the same unique code.

Error:

EXCEPTION_THROWN|[47]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for this type of entity through the api: Linked Entity ID: [LinkedEntityId].

public with sharing class TRG_Handler_ContentVersion {

public static void xxxx(List<ContentVersion> desencadenador){

    List<String> listaDesencadenador = new List<String> ();
    for(ContentVersion aux:desencadenador){
        listaDesencadenador.add(aux.FirstPublishLocationId);
    }

    List<Factura__c> esFactura = new List<Factura__c>([SELECT FAC_TXT_Numero_factura__c FROM Factura__c WHERE Id IN :listaDesencadenador]);
    if(esFactura.size() == 1){         

       List<Factura__c> facturasHermanas= new List<Factura__c>([SELECT Id FROM Factura__c WHERE FAC_TXT_Numero_factura__c = :esFactura[0].FAC_TXT_Numero_factura__c]); 
       if(facturasHermanas.size() > 1){ 
       List<ContentVersion> contentVersionId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:desencadenador[0].Id];
       List<ContentDocumentLink> facturasRelacionadas = new List<ContentDocumentLink>();
       ContentDocumentLink addLista = new ContentDocumentLink();

       for(Factura__c factura:facturasHermanas){
       if(desencadenador[0].FirstPublishLocationId!=factura.Id)
       {
       //addLista = new ContentDocumentLink(ContentDocumentId = desencadenador[0].ContentDocumentId,    LinkedEntityId = factura.Id, ShareType='C', Visibility='SharedUsers');
       addLista = new ContentDocumentLink(ContentDocumentId = contentVersionId[0].ContentDocumentId,    LinkedEntityId = factura.Id, ShareType='C', Visibility='SharedUsers');
       facturasRelacionadas.add(addLista);
       }
       }
       //insert facturasRelacionadas;
       upsert facturasRelacionadas;
       }
       }}
  • Solved! API versión 41 in EndPoint :) – Blanca PJ Mar 6 '18 at 15:23
  • Please update your target URL to 41.0 and check. – Soumyaranjan Pati Apr 6 '18 at 6:54

Your Answer

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

Browse other questions tagged or ask your own question.