I have custom metadata type with api name program_mdt in which I have created two fields called Email_Keyword__c and Body__c. In the below trigger I am trying to get rid of hard coded values in the if(filename.contains('report')) and the defining body of the email in step4.
I am not sure how to proceed from here to replace hard coded values with custom metadata. In this case i am trying to send out an email when file name contains the values that exist in Email_Keyword__c field of program__mdt.
trigger FilesAccountUpload_Tr on ContentDocumentLink (before insert) {
set<Id>setAccIds = new set<Id>();
set<Id>setCntDocIds = new set<Id>();
List<Program__mdt> lstMetaData = [SELECT Email_c FROM Program__mdt];
Map<Id, String> filestitlemaps = new Map<Id, String>();
Map<Id, String> accnamemaps = new Map<Id, String>();
for(ContentDocument c : [select id, title from ContentDocument]){
filestitlemaps.put(c.id,c.title);
}
for(Appointment__c app: [select id,Name from Appointment__c]){
accnamemaps.put(app.id,app.name);
}
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Set<String> KeywordSet = new Set<String>();
for(ContentDocumentLink clIterator : trigger.new) {
string strObjPrefix = String.valueOf(clIterator.LinkedEntityId).substring(0, 3);
if(strObjPrefix == Appointment__c.sObjectType.getDescribe().getKeyPrefix()) {
setCntDocIds.add(clIterator.ContentDocumentId);
setAccIds.add(clIterator.LinkedEntityId);
string filename =filestitlemaps.get(clIterator.ContentDocumentId);
string accountname = accnamemaps.get(clIterator.LinkedEntityId);
Set<String> KeywordSet = new Set<String>();
if(filename.contains('report')){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add('test@gmail.com');
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('test@gmail.com');
mail.setSenderDisplayName('File gets uploaded');
// (Optional) Set list of people who should be CC'ed
List<String> ccTo = new List<String>();
ccTo.add('test@gmail.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject(accountname+'File uploaded');
String body = 'Dear Team , ';
//body += +filename+'files was uploaded';
body += +Program_Setting__mdt.Email_Body__c;
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
// Step 6: Send all emails in the master list
Messaging.sendEmail(mails);
}
}
}
}