0

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.

Here is what I have tried.

trigger CheckFile on ContentDocumentLink (before insert) {
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    
    Set<ID> parentIds = new Set<ID>();
    Set<ID> NotEligible = new Set<ID>();
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    

    List<Menu__c> menuIds = [SELECT Id FROM Menu__c WHERE Id IN :parentIds];
    
    
    for (Menu__c pk : menuIds){
            NotEligible.add(pk.id); 
                        } 
    
    System.debug(NotEligible);
    
    for (ContentDocumentLink cdls : Trigger.new) {
           if(NotEligible.contains(cdls.LinkedEntityId))
             cdls.addError('You cannot insert an image record.');
        }
    
 

}

I have a file associated with each record of a custom object. If the image file is there for the record, I do not want to be able to add more image files to it. Else, I want to be able to add a picture.

I have a file associated with each record of a custom object. If the image file is there for the record, I do not want to be able to add more image files to it. Else, I want to be able to add a picture.

2 Answers 2

0

You are currently just querying all parent records. Those will always be there. You probably want to query for child records belonging to those parents. I.e. instead of

List<Menu__c> menuIds = [SELECT Id FROM Menu__c WHERE Id IN :parentIds];

You will want to do something like

List<ContentDocumentLink> existingCdlParentIds = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN :parentIds];

This way you'll find all parentids with existing files.

Then change your loop to check through these existing ContentDocumentLinks and gather the parentid's (LinkedEntityID's)

//for (Menu__c pk : menuIds){
//        NotEligible.add(pk.id); 
//                    } 
//                    
for (ContentDocumentLink cdl : existingCdlParentIds){
    NotEligible.add(cdl.LinkedEntityId);
}

Then possibly add a filter for parent record type, if that is needed.

8
  • It throws an error : No such column 'ParentId' on entity 'ContentDocumentLink'. I am a complete beginner and have no idea how ContentDocumentLink, LinkedEntityId etc works. If you have any resources, do share. Feb 16, 2022 at 10:20
  • sorry I meant LinkedEntityId. Corrected. Feb 16, 2022 at 10:22
  • 1
    You can find the ContenDocumentLink description in the Salesforce documentation: developer.salesforce.com/docs/atlas.en-us.api.meta/api/… Feb 16, 2022 at 11:05
  • 1
    Also, please describe what's not working for you so we can help you further with that. You can do that by updating your question. Feb 16, 2022 at 11:06
  • 1
    See my updated answer. And please post (as an edit to your question) what you've tried. It's working fine for me. Feb 16, 2022 at 15:38
0

I finally got the solution to the problem.

trigger CheckFile on ContentDocumentLink (before insert) {
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    
    Set<ID> parentIds = new Set<ID>();
    Set<ID> NotEligible = new Set<ID>();
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    
    List<ContentDocumentLink> refinedContentDocumentList = new List<ContentDocumentLink>();
    List<ContentDocumentLink> existingCdlParentIds = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN :parentIds];
    
    for(ContentDocumentLink cdLink : existingCdlParentIds){
        if(String.valueOf(cdLink.LinkedEntityId.getSObjectType()) != 'User'){
            refinedContentDocumentList.add(cdLink);
        }
    }
    
    for (ContentDocumentLink cdl : refinedContentDocumentList){
    NotEligible.add(cdl.LinkedEntityId);
}
  
    
    for (ContentDocumentLink cdls : Trigger.new) {
           if(NotEligible.contains(cdls.LinkedEntityId))
             cdls.addError('You cannot insert an image record.');
        }
    
 

}
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 17, 2022 at 5:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .