function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumant KuchipudiSumant Kuchipudi 

How can we restrict to one File/Note upload only on Custom object

I have custom object and it has Notes and Files related list, I want to restrict to one Note and one File for each custom record. Would that be possible?
Raj VakatiRaj Vakati
You need to write the trigger on both Note and ContentDocument .. 

Sample code for notes 
 
List<Note> notes =(List<Note>)Trigger.new ;
        Map<Id,Id> relatedData = new Map<Id,Id>() ;    
        for(Note c : notes){
            if(String.valueOf(c.ParentId).startsWith('00Q') ){
                relatedData.put(c.Id, c.ParentId);
            }
        }
        if(relatedData.values().size()>0){
            // Query the Leads 
            Map<Id , Lead> leads = new  Map<Id , Lead>([Select Id ,Name,Owner.Id,Owner.Name,CreatedBy.Name ,Owner.Email  from Lead where id =:relatedData.values() ]) ;
            for(Note c : notes){
                if(String.valueOf(c.ParentId).startsWith('00Q')){
                // Handler Errors here  
                }
            }
        }

ContentDocument  SOQL for same 
 
[SELECT Id, LinkedEntityId, ContentDocumentId,Visibility, IsDeleted, ShareType,ContentDocument.Title,ContentDocument.createdDate, ContentDocument.FileType FROM ContentDocumentLink WHERE LinkedEntityId =:<CONTACT_ID> ];