Last active
February 22, 2023 09:23
-
-
Save salesforce-casts/a1fd9e5be9ee020914ceba0ba6e546a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@InvocableApex | |
public void processEmailWithAttachment(){ | |
//Attachement Part | |
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); | |
Map<Id,ContentDocumentLink> mapContDocuLink = new Map<Id,ContentDocumentLink>([Select Id, ContentDocumentId FROM | |
ContentDocumentLink | |
WHERE LinkedEntityId=:oppDetails.Id ]); | |
Set<Id> setContDocumentId =new Set<Id>(); | |
for (ContentDocumentLink docLinks :mapContDocuLink.values()) { | |
setContDocumentId.add(docLinks.ContentDocumentId); | |
} | |
if(!setContDocumentId.isEmpty()){ | |
Map<Id,ContentDocument> mapContDocument= new Map<Id,ContentDocument>([Select Id, FileType, Title, FileExtension FROM ContentDocument | |
WHERE Id IN :setContDocumentId]); | |
Map<Id,ContentVersion> mapContVersion = new Map<Id,ContentVersion>([Select Id,ContentDocumentId, VersionData FROM ContentVersion | |
WHERE ContentDocumentId IN :setContDocumentId]); | |
Map<Id,ContentVersion> mapContVerWithContDocId = new Map<Id,ContentVersion>(); | |
if(!mapContVersion.isEmpty()){ | |
for (ContentVersion contVerRec :mapContVersion.values()) { | |
mapContVerWithContDocId.put(contVerRec.ContentDocumentId,contVerRec); | |
} | |
} | |
if(!mapContVerWithContDocId.isEmpty() && (!mapContDocument.isEmpty())){ | |
for (Id contDocumentId : setContDocumentId) { | |
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); | |
String fileName = mapContDocument.get(contDocumentId).Title.Trim() + '.' + mapContDocument.get(contDocumentId).FileExtension.Trim(); | |
efa.setFileName(fileName); //Title of the PDF | |
efa.setBody(mapContVerWithContDocId.get(contDocumentId).VersionData); //Body of the PDF,need to do transfer into blob | |
fileAttachments.add(efa); | |
} | |
} | |
} | |
if(fileAttachments.size()>0){ | |
mail.setFileAttachments(fileAttachments); | |
} | |
mails.add(mail); | |
List<Messaging.SendEmailResult> results= Messaging.sendEmail(mails); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment