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
Rakin MohammedRakin Mohammed 

How to create trigger on metadata to detect autoreplies?

Hey guys, I've been tasked with the responsibility of creating a trigger to detect autoreply emails for cases created in salesforce. To do this, I have created a custom metadata object that multiple records of special words that can indicate an autoreply. Can someone tell me if my code is functioning properly, by adding the metadata records to a string set, then comparing the email string to the set? If not, can anyone point me in the right direction, thanks. 
private static void detectAutoresponse(){
    Autoresponse_Blacklist_mdt[] errorRecords = [SELECT MasterLabel, Body_Text_c from Autoresponse_Blacklist_mdt];
    Set<String> errorWords = new Set<String>;
    for(Autoresponse_Blacklist_mdt record: errorRecords){
        errorWords.add(record.Body_Text_c);
    }

    for(Case thisCase : (List<Case>)Trigger.new){
        for(Sting words: errorWords){
            if(thisCase.Contain == words)
                thisCase.addError('Not saving Automatic Reply');
        }    
    }
}

 
ShirishaShirisha (Salesforce Developers) 
Hi Rakin,

Greetings!

Can you please troubleshoot the code by capturing the debug logs to see,if it is able to function correctly as it is difficult to confirm,if the code is working or not by looking at code only.

Reference:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_debugging_debug_log.htm

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri