Skip to main content
Roman Regmi (CQ Nepal) 님이 #Apex에 질문했습니다
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!!
0/9000