You need to sign in to do that
Don't have an account?
Hanna Bergsma
Modify existing trigger to attach multiple related Files to email
I have the working trigger below that attaches whatever document that is in the Files related list to an email and sends it the contact on a case.
If there is more than one doc in the Files realted list, I get an error. Can anyone advise on how to modify the code to add all related list docs to the email?
If there is more than one doc in the Files realted list, I get an error. Can anyone advise on how to modify the code to add all related list docs to the email?
trigger SendContentVersion on Case (after update) { String subject = 'Placeholder'; String body = 'Placeholder'; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); for(Case myCase : trigger.new){ Case oldCase = Trigger.oldMap.get(myCase.Id); // Get ContentVersion ContentDocumentLink a = [SELECT Id, LinkedEntityId, ContentDocumentId, Visibility, IsDeleted, ShareType, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.FileType FROM ContentDocumentLink WHERE LinkedEntityId =: myCase.id]; Id contentDocumentId = a.ContentDocumentId; ContentVersion[] cv_list = [select VersionData, PathOnClient from ContentVersion where ContentDocumentId =: contentDocumentId]; ContentVersion cv = new ContentVersion(); if(cv_list.size() != 0) { cv = cv_list[0]; // Create the email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName(cv.pathOnClient); efa.setBody(cv.versionData); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); } //add address's that you are sending the email to List<String> sendTo = new List<String>(); String owneremail = [select Email from User where Id = :myCase.OwnerId].Email; sendTo.add(myCase.ContactEmail); sendTo.add(owneremail); email.setSubject(subject); String[] toAddresses = (sendto); email.setToAddresses( toAddresses ); email.setPlainTextBody(body); // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } } }
Please follow the below link:- https://developer.salesforce.com/forums/?id=906F0000000904xIAA
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
Apex Trigger :
trigger ContentDocumnetLInkTrigger on ContentDocumentLink (After insert) {
ContentDocumnetLInkTriggerHelper.sendMultipleFile(Trigger.newMap, Trigger.oldMap);
}
Apex Helper Classs:
public class ContentDocumnetLInkTriggerHelper {
public static void sendMultipleFile(Map<Id, ContentDocumentLink> neMap,
Map<Id, ContentDocumentLink> oldMap) {
system.debug('newMap values---->'+neMap);
String subject = 'Placeholder';
String body = 'Placeholder';
Boolean isSuccess = false;
Set<Id> caseIds = new Set<Id> ();
Map<String, Case> mapOfContact = new Map<String, Case>();
Set<Id> contentDocumentId = new Set<Id> ();
for (ContentDocumentLink getRecords : neMap.Values()) {
if (getRecords.LinkedEntityId != Null ) {
caseIds.add(getRecords.LinkedEntityId);
contentDocumentId.add(getRecords.ContentDocumentId);
}
}
for(Case cclist : [SELECT Id, ContactEmail, CaseNumber FROM
Case WHERE Id IN : caseIds] ) {
mapOfContact.put(cclist.ContactEmail, cclist);
}
List<ContentVersion> cv_list = [SELECT VersionData, PathOnClient FROM
ContentVersion WHERE
ContentDocumentId IN : contentDocumentId ];
system.debug('size list--->'+cv_list);
String[] toAddresses = new String[]{};
for (String s : mapOfContact.keySet()) {
system.debug('s--->'+s);
toAddresses.add(s);
}
if (cv_list.size() > 0) {
system.debug('cv_list');
for (ContentVersion cv : cv_list) {
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(cv.pathOnClient);
efa.setBody(cv.versionData);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(toAddresses);
mail.setCCAddresses(toAddresses);
mail.setSubject(subject);
mail.setHtmlBody('<h1>Dear Shatrughna Salunke</h1>');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });
try {
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(results[0].success) {
system.debug('isSucess--->'+isSuccess);
isSuccess = true;
}
else {
isSuccess = false;
system.debug('isSucess--->'+isSuccess);
}
}
catch(Exception ex) {
isSuccess = false;
}
}
}
}
}
Regards,
Shatrughna