You need to sign in to do that
Don't have an account?

Cover test class for querying template and sending it through flow
Hi,
Can anyone tell me how to cover the get 100% coverage for the below class
global class SNL_002A_EmailAlert {
@InvocableMethod
public static void download(List<Order> OrderId) {
set<id> ordId = new set<id>();
for (Order s:OrderId){
ordId.add(s.Id);
}
List<Order> OrderList =[Select Id,BillToContactId from Order where Id IN : ordId];
String templateId, whoId, whatId;
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate( templateId, whoId, whatId);
/**** Attach files to the message ****/
List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
List<id> ContentDocumentids = new List<id>();
for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid IN : ordId]){
{
ContentDocumentids.add(CDLink.ContentDocumentid);
}
system.debug('ContentDocumentids'+ContentDocumentids);
for ( ContentVersion cversion : [SELECT title,
PathOnClient,
versiondata
FROM contentversion
WHERE ContentDocumentId IN :ContentDocumentids
])
{
blob WOCFbody = cversion.versiondata;
system.debug('body : '+WOCFbody+'-----------'+cversion.title);
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(cversion.title);
efa.setBody(WOCFbody);
attachments.add(efa);
}
}
email.setFileAttachments(attachments);
/******* Set the message template ******/
String tempName = String.valueOf(System.Label.SNL_002A_Template);
List<EmailTemplate> templates = [SELECT Id, Subject,Markup,body, HtmlValue FROM EmailTemplate WHERE DeveloperName =:tempName];
if (!templates.isEmpty()) {
templateId = templates[0].Id;
whoId = OrderList[0].BillToContactId;
whatId = OrderId[0].Id;
email.setTargetObjectId(whoId);
email.setSubject(email.getSubject());
email.sethtmlBody(email.gethtmlBody());
email.saveAsActivity = false;
}
/*******Send the message *******/
try {
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
} catch (Exception e) {
throw e;
}
}
}
Can anyone tell me how to cover the get 100% coverage for the below class
global class SNL_002A_EmailAlert {
@InvocableMethod
public static void download(List<Order> OrderId) {
set<id> ordId = new set<id>();
for (Order s:OrderId){
ordId.add(s.Id);
}
List<Order> OrderList =[Select Id,BillToContactId from Order where Id IN : ordId];
String templateId, whoId, whatId;
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate( templateId, whoId, whatId);
/**** Attach files to the message ****/
List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
List<id> ContentDocumentids = new List<id>();
for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid IN : ordId]){
{
ContentDocumentids.add(CDLink.ContentDocumentid);
}
system.debug('ContentDocumentids'+ContentDocumentids);
for ( ContentVersion cversion : [SELECT title,
PathOnClient,
versiondata
FROM contentversion
WHERE ContentDocumentId IN :ContentDocumentids
])
{
blob WOCFbody = cversion.versiondata;
system.debug('body : '+WOCFbody+'-----------'+cversion.title);
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(cversion.title);
efa.setBody(WOCFbody);
attachments.add(efa);
}
}
email.setFileAttachments(attachments);
/******* Set the message template ******/
String tempName = String.valueOf(System.Label.SNL_002A_Template);
List<EmailTemplate> templates = [SELECT Id, Subject,Markup,body, HtmlValue FROM EmailTemplate WHERE DeveloperName =:tempName];
if (!templates.isEmpty()) {
templateId = templates[0].Id;
whoId = OrderList[0].BillToContactId;
whatId = OrderId[0].Id;
email.setTargetObjectId(whoId);
email.setSubject(email.getSubject());
email.sethtmlBody(email.gethtmlBody());
email.saveAsActivity = false;
}
/*******Send the message *******/
try {
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
} catch (Exception e) {
throw e;
}
}
}
The code provided in question does not highlight the uncovered lines in bold. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Examples of process builder/sendEmail test classes:
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
https://salesforce.stackexchange.com/questions/227162/test-class-help-send-email-class/227165
https://developer.salesforce.com/forums/?id=9060G000000BibcQAC
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Can you please share screen shot of running test class
hi,
do some needful changes according to your code.
Please mark it as the Best Answer so that other people would take references from it .