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
Jonathan Wolff 7Jonathan Wolff 7 

Change apex trigger so it does not block file types but makes them the only allowed

Hello, I build a trigger that blocks file types that are in my custom metadata (DataBlock). Now I would like to change the trigger so every type except the ones in my metadata are blocked. Could you tell me by my code sample how to change the apex trigger so it does not block these file types but allows them and blocks every else?

Greetings
Jonathan

trigger DataBlockContentVersion on ContentVersion(before insert) {
    try {   
        Set<String> blockedTypes = new Set<String>();
        for (DataBlock__mdt  blockedType : [SELECT FileExtension__c FROM DataBlock__mdt WHERE FileExtension__c != null]) {
            blockedTypes.add(blockedType?.FileExtension__c?.toUpperCase());
        }
        for (ContentVersion myDocument : Trigger.new) {
            system.debug('PathOnClient '+myDocument.PathOnClient);
                
            if (myDocument.PathOnClient != null) {
                String[] parts = myDocument.PathOnClient.split('\\.'); /*Payal: Updated regex for splitting extension*/
                system.debug('part '+parts);
                if (parts.size() > 0) {
                    String ext = parts[parts.size() - 1];
                    if (blockedTypes.contains(ext.toUpperCase())) {
                       myDocument.addError('The datatype ' + ext + ' is not allowed'); 
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new StringException(e.getMessage() + ' - ' + e.getLineNumber() + '\r\n' + e.getStackTraceString());
    }
}
Best Answer chosen by Jonathan Wolff 7
Yogendra JangidYogendra Jangid
Hi Jonathan,
please try below code and this will block every other file type which is not in your metadata DataBlock__mdt
trigger DataBlockContentVersion on ContentVersion(before insert) {
    try {   
        Set<String> blockedTypes = new Set<String>();
        for (DataBlock__mdt  blockedType : [SELECT FileExtension__c FROM DataBlock__mdt WHERE FileExtension__c != null]) {
            blockedTypes.add(blockedType?.FileExtension__c?.toUpperCase());
        }
        for (ContentVersion myDocument : Trigger.new) {
            system.debug('PathOnClient '+myDocument.PathOnClient);
                
            if (myDocument.PathOnClient != null) {
                String[] parts = myDocument.PathOnClient.split('\\.'); /*Payal: Updated regex for splitting extension*/
                system.debug('part '+parts);
                if (parts.size() > 0) {
                    String ext = parts[parts.size() - 1];
                    if (!blockedTypes.contains(ext.toUpperCase())) {
                       myDocument.addError('The datatype ' + ext + ' is not allowed'); 
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new StringException(e.getMessage() + ' - ' + e.getLineNumber() + '\r\n' + e.getStackTraceString());
    }
}
please let me know if this answers your question and if so please can you mark this as best answer.