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
Andrea Moinet-NicholsAndrea Moinet-Nichols 

Apex Trigger to lock files upload when box checked.

Hello,

I have created a custom field (checkbox) under the opportunity object. I would like to prevent any files from being uploaded to the opportunity if the box is checked. I am having trouble creating the apex trigger. 
Best Answer chosen by Andrea Moinet-Nichols
Maharajan CMaharajan C
Hi Andrea,

You are writing the trigger on wrong object (Opportunity). And also i have used Signed__c as custom field (checkbox) under the opportunity object in below code so please replace this field with your custom field api name from Opportunity.

Please write it in ContentDocumentLink object. 
 
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    set<Id> setOppIds = new set<Id>();
    for(ContentDocumentLink cdl : Trigger.New) {
        String strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
        if(strObjPrefix == Opportunity.sObjectType.getDescribe().getKeyPrefix()) {
            setOppIds.add(cdl.LinkedEntityId);
        }
    }
    
    if(!setOppIds.isEmpty()){
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Signed__c from Opportunity where ID IN: setOppIds]);
        
        for(ContentDocumentLink cdl : Trigger.New){
            if(oppMap.ContainsKey(cdl.LinkedEntityId)){
                Boolean isSigned = oppMap.get(cdl.LinkedEntityId).Signed__c;
                if(isSigned){
                    cdl.addError('File Upload is restricted in Signed Opportunity');
                }
            }
        }
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Andrea,

I have Signed__c field as custom checkbox field from Opportunity object in below code so please change the api name as per your org.

Please use the below trigger:
 
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    set<Id> setOppIds = new set<Id>();
    for(ContentDocumentLink cdl : Trigger.New) {
        String strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
        if(strObjPrefix == Opportunity.sObjectType.getDescribe().getKeyPrefix()) {
            setOppIds.add(cdl.LinkedEntityId);
        }
    }
    
    if(!setOppIds.isEmpty()){
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Signed__c from Opportunity where ID IN: setOppIds]);
        
        for(ContentDocumentLink cdl : Trigger.New){
            if(oppMap.ContainsKey(cdl.LinkedEntityId)){
                Boolean isSigned = oppMap.get(cdl.LinkedEntityId).Signed__c;
                if(isSigned){
                    cdl.addError('File Upload is restricted in Signed Opportunity');
                }
            }
        }
    }
}


In Classic you will recieve the same add error message but in Lightning page we have the Salesforce Known issue on Displaying the custom message from file upload... Instead of custom message you recieve the common message in Lightning.

https://trailblazer.salesforce.com/issues_view?id=a1p3A000000mDKdQAM&title=custom-error-messages-not-displayed-in-lightning-experience-when-uploading-files (https://trailblazer.salesforce.com/issues_view?id=a1p3A000000mDKdQAM&title=custom-error-messages-not-displayed-in-lightning-experience-when-uploading-files)

https://www.salesforcecodecrack.com/2019/02/salesforce-contentdocumentlink-trigger.html

Thanks,
Maharajan.C
Andrea Moinet-NicholsAndrea Moinet-Nichols
Hello and thank you Maharajan!

I have created the trigger as below, but I am getting an error "Variable does not exist: LinkedEntityId"

trigger ControlledGoodsTrigger on Opportunity (before insert) {
    set<Id> setOppIds = new set<Id>();
    for(Opportunity cdl : Trigger.New) {
        String strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
        if(strObjPrefix == Opportunity.sObjectType.getDescribe().getKeyPrefix()) {
            setOppIds.add(cdl.LinkedEntityId);
        }
    }
    
    if(!setOppIds.isEmpty()){
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Controlled_Goods__c from Opportunity where ID IN: setOppIds]);
        
        for(Opportunity cdl : Trigger.New){
            if(oppMap.ContainsKey(cdl.LinkedEntityId)){
                Boolean isSigned = oppMap.get(cdl.LinkedEntityId).Controlled_Goods__c;
                if(isSigned){
                    cdl.addError('File Upload is restricted in Signed Opportunity');
                }
            }
        }
    }
}
Maharajan CMaharajan C
Hi Andrea,

You are writing the trigger on wrong object (Opportunity). And also i have used Signed__c as custom field (checkbox) under the opportunity object in below code so please replace this field with your custom field api name from Opportunity.

Please write it in ContentDocumentLink object. 
 
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    set<Id> setOppIds = new set<Id>();
    for(ContentDocumentLink cdl : Trigger.New) {
        String strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
        if(strObjPrefix == Opportunity.sObjectType.getDescribe().getKeyPrefix()) {
            setOppIds.add(cdl.LinkedEntityId);
        }
    }
    
    if(!setOppIds.isEmpty()){
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Signed__c from Opportunity where ID IN: setOppIds]);
        
        for(ContentDocumentLink cdl : Trigger.New){
            if(oppMap.ContainsKey(cdl.LinkedEntityId)){
                Boolean isSigned = oppMap.get(cdl.LinkedEntityId).Signed__c;
                if(isSigned){
                    cdl.addError('File Upload is restricted in Signed Opportunity');
                }
            }
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Andrea Moinet-NicholsAndrea Moinet-Nichols
Hello Maharajan,

Thank you so much, this works great.

trigger ControlledGoods on ContentDocumentLink (before insert) {
    set<Id> setOppIds = new set<Id>();
    for(ContentDocumentLink cdl : Trigger.New) {
        String strObjPrefix = String.valueOf(cdl.LinkedEntityId).substring(0, 3);
        if(strObjPrefix == Opportunity.sObjectType.getDescribe().getKeyPrefix()) {
            setOppIds.add(cdl.LinkedEntityId);
        }
    }
    
    if(!setOppIds.isEmpty()){
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select Id,Controlled_Goods__c from Opportunity where ID IN: setOppIds]);
        
        for(ContentDocumentLink cdl : Trigger.New){
            if(oppMap.ContainsKey(cdl.LinkedEntityId)){
                Boolean isSigned = oppMap.get(cdl.LinkedEntityId).Controlled_Goods__c;
                if(isSigned){
                    cdl.addError('File Upload is restricted in Signed Opportunity');
                }
            }
        }
    }
}

I have created the change sets in both my production and sandboxe org. Would you be able to help me with the Test Class?

Thank you very much in advance.
dawntr uetesasdawntr uetesas
You will love their authenticity and optimism. Such wives will make your mail order wives (https://elitemailorderbrides.com/married life positive and energetic. You will not be bored, because your every day will be unusual. These women have strong characters, but they are also very passionate and kind. Your married life will be filled with comfort and happiness because your wife will love you and your children.