Skip to main content
I have a scenario where I need to check if a file has been added to a custom object record. If a file is already present, then the user should not be able to upload more files. Else, the user should be able to upload files.
2 个回答
  1. 2022年2月15日 10:45
    Hi Roman,

    Try with below code and replace your custom object name.

    trigger documentcheck on ContentDocumentLink (before insert) {

    Set<Id> parentQuoteDocumentIds = new Set<Id>();

    for(ContentDocumentLink cdl : Trigger.new){

    parentQuoteDocumentIds.add(cdl.LinkedEntityId);

    }

    List<ContentDocumentLink> CDLList = new List<ContentDocumentLink>([SELECT id, LinkedEntityId fROM ContentDocumentLink WHERE LinkedEntityId IN : parentQuoteDocumentIds]);

    for(ContentDocumentLink a : Trigger.new) {

    if(CDLList.size()>0 && (a.LinkedEntityId).getSObjectType().getDescribe().getName() == 'Customobject__c' ) {

    a.addError('You cannot add More than one file');

    }

    }

    }

    If this helps, Please mark it as best answer.

    Thanks!!