0

I have an object 'ClientContract__c' with affiliated files. When adding a file, this changes a technical checkbox field on the parent object. I would like to override this technical field if there are no more affiliate files.

When deleting a file, the same trigger is not called (when updating and inserting, the trigger call is on ContentDocumentLink object while when deleting it is a trigger on the ContentDocument object).

Can you help me to Retrieve the ContentDocumentLink from the trigger on the ContentDocument object and if there is no file pass my technical field to false?

My code on ContentDocument object :

if(Trigger.isDelete){
    Set<Id> SetOfCd = new Set<Id>();
        List<ID> opportID = new List<ID>();
    for(ContentDocument cd :Trigger.old){
        SetOfCd.add(cd.Id);
    }

    for (ContentDocumentLink files: [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:SetOfCd]) {
        if(!(opportID.contains(files.LinkedEntityId)))
            opportID.add(files.LinkedEntityId);
    }

    List<ContratClient__c> cc = [SELECT Id,FichierLie__c FROM ContratClient__c WHERE Id IN :opportID];
}

My code on ContentDocumentLink object when the file is upload :

if(Trigger.isInsert){
        List<ID> opportID = new List<ID>();

        for (ContentDocumentLink files: Trigger.new) {
            if(!(opportID.contains(files.LinkedEntityId)))
                opportID.add(files.LinkedEntityId);
        }

        List<ContratClient__c> cc = [SELECT Id,FichierLie__c FROM ContratClient__c WHERE Id IN :opportID];

        for (ContratClient__c contrat :cc) {
                contrat.FichierLie__c = true;
        }

        update cc;
        system.debug('Contrat:' + cc);

    }
0

If you are using Salesforce Classic, then you will have to write trigger on ContentDocumentLink and not ContentDocument, as Salesforce does not delete ContentDocument while in classic mode, hence delete trigger of ContentDocument would not be fired.

Here is the article mentioning the same

So move your code from ContentDocument Trigger to ContentDocumentLink trigger. That should solve your problem.

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.