+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Deployment fail for apex trigger with custom metadata

Hello, I wanted to deploy an apex trigger, but I got following error:User-added image
Could you tell me what to change in my apex code or custom metadata, so this error will not appear and the deployment will work?


trigger DataBlockContentVersion on ContentVersion(before insert) {
try {   
    //DataBlock_mdt = Im Bereich Custom Metadata Types: Alle Dateitypen die als Files hochgeladen werden können
        //Set<String> blockedTypes = new Set<String>();
        Set<String> allowedTypes = new Set<String>();
        for (DataBlock__mdt  blockedType : [SELECT FileExtension__c FROM DataBlock__mdt WHERE FileExtension__c != null]) {
            allowedTypes.add(blockedType?.FileExtension__c?.toUpperCase());
        }
        for (ContentVersion myDocument : Trigger.new) {
            system.debug('PathOnClient '+myDocument.PathOnClient);
                
            if (myDocument.PathOnClient != null) {
                String[] parts = myDocument.PathOnClient.split('\\.'); 
                system.debug('part '+parts);
                if (parts.size() > 0) {
                    String ext = parts[parts.size() - 1];
                    if (!allowedTypes.contains(ext.toUpperCase())) { // added a NOT operator ! at the front
                       myDocument.addError('Der Upload von Dateien mit der Endung .' + ext + ' ist nicht erlaubt. Erlaubte Dateitypen sind .pptx, .xlsx, .docx, .msg, .pdf'); 
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new StringException(e.getMessage() + ' - ' + e.getLineNumber() + '\r\n' + e.getStackTraceString());
    }
}

User-added image
Best Answer chosen by Jonathan Wolff 7
Yogendra JangidYogendra Jangid
Hi Jonathan,
I trust you might have added the custom metadata type in your change set as per documentation 
https://help.salesforce.com/articleView?id=sf.custommetadatatypes_changesets.htm&type=5 (https://help.salesforce.com/articleView?id=sf.custommetadatatypes_changesets.htm&type=5)

but you might have missed to add the custom field (FileExtension__c in your case) and corresponding page layout as well for this custom setting. Please add them and deploy again. 

Hope this will resolve your error, and if so please can you mark this as best answer. Thanks.

All Answers

mukesh guptamukesh gupta
Hi Jonathan,

Make sure that this field is actually visible to your user via Field Level Security.
Click the field name in your Setup UI to view the field definition detail.
On this detail view, there will be a Set Field-Level Security button.
Make sure Visible is selected for your Profile.

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Yogendra JangidYogendra Jangid
Hi Jonathan,
I trust you might have added the custom metadata type in your change set as per documentation 
https://help.salesforce.com/articleView?id=sf.custommetadatatypes_changesets.htm&type=5 (https://help.salesforce.com/articleView?id=sf.custommetadatatypes_changesets.htm&type=5)

but you might have missed to add the custom field (FileExtension__c in your case) and corresponding page layout as well for this custom setting. Please add them and deploy again. 

Hope this will resolve your error, and if so please can you mark this as best answer. Thanks.
This was selected as the best answer