1

I need to prevent uploading file in Notes & Attachments when parent record's status is set to some value. ex: If parent record's status values is Submitted then user should not be able to upload attachment.

Any suggestions please.

trigger Attachmenterror2 on ContentDocument (before insert,before delete) {
Schema.DescribeSObjectResult inv = Expense__c.sObjectType.getDescribe();
String invKeyPrefix = inv.getKeyPrefix();
List<Id> contentDocId = new List<Id>();

Map<Id, Id> contDocLinkedMap = new Map<Id, Id>();
Id pId;

if(trigger.IsInsert){
    for(ContentDocument att: Trigger.new){
        pId=att.id; 
        contentDocId.add(att.Id);
    }
    for(ContentDocumentLink cdl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN : contentDocId]){
        contDocLinkedMap.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
        System.debug('map ' +contDocLinkedMap);
    }
    for(ContentDocument att: Trigger.new){ 

        if(invKeyPrefix == String.valueOf(contDocLinkedMap.get(att.Id)).left(3) &&
           [select Client__c from Expense__c where id = :contDocLinkedMap.get(att.Id)].Client__c =='asdf' && att.Title.Startswith('Dep') ){
              System.debug('contDocLinkedMap.get(att.Id)].Client__c+++ ' +contDocLinkedMap.get(att.Id));
               att.adderror('This attachment cannot be inserted');
           }

    }
}     

if(trigger.IsDelete){
    for(ContentDocument con : Trigger.old){
        System.debug(Trigger.old);
        System.debug(con.Id);
        contentDocId.add(con.Id);
    }
    for(ContentDocumentLink cdl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN : contentDocId]){
        contDocLinkedMap.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
        System.debug('map ' +contDocLinkedMap);
    }
    for(ContentDocument cdoc : Trigger.Old){
         System.debug('map+++++= ' +contDocLinkedMap);
        if(invKeyPrefix == String.valueOf(contDocLinkedMap.get(cdoc.Id)).left(3) &&
           [select Client__c from Expense__c where id = :contDocLinkedMap.get(cdoc.Id)].Client__c =='asdf' && cdoc.Title.Startswith('Dep') ){
               System.debug('map+++++= ' +contDocLinkedMap);
               cdoc.adderror('This attachment cannot be deleted');
           }
    }
}

}

I am getting below error while inserting.

Error:

System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.

I have written above code. But i need to stop or delete file name starts with particular name. like if document name start with mike then i should not allow. for other documents they can insert or delete. There is no field called name to prefix on ContentDocumentLink. I have to make it work in lighting. Any idea?

  • 1
    You may consider writing a trigger on the attachment object. Fetch the record from the ParentId field, and if the status of the parent record is submitted, you can prevent the file from being uploaded by showing an error in the trigger. – Ayush Goyal Dec 20 '19 at 11:16
  • Hi Ayush Goyal. i have tried but can you help me how to prefix the file name? I have updated my code please have a look. It is not working while deleting. – Frank Dec 20 '19 at 12:25
  • Hi Frank, have you tried the solution posted in the answer below? – Ayush Goyal Dec 20 '19 at 14:07
  • Hi I have tried but i am getting error while inserting attachment. can't we get contentDocumentid from Contentdocumentlink record? i am getting null for in contentdocumentlink loop for trigger.new. It would be great help if you can make me understand what is happening. Updated code – Frank Dec 20 '19 at 14:17
  • Rather than using List to store Id(s), can you try using Set and run the query again? That should fix the issue. – Ayush Goyal Dec 21 '19 at 3:29
3

The ContentDocumentLink object has the ContentDocumentId reference field. You should query the related Content Document record and its Title field. Then you could use String class' method .startsWith to check whether or not the file starts with the name you specify.

| improve this answer | |
  • Code updated.... – Frank Dec 20 '19 at 14:18

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.