You need to sign in to do that
Don't have an account?
Andrea 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.
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.
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.
Thanks,
Maharajan.C
All Answers
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:
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
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');
}
}
}
}
}
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.
Thanks,
Maharajan.C
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.