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

test class for sending Emails
Hi,
I,m unable to get code coverage for sending emailnotification. can anyone help me. Thanks in advance
public class NotificationEmail {
public static void sendNotificationEmail(List<Account> newList2){
map<id,set<string>> AccRecp = new map<id,set<string>>();
for(Account acc2 : newList2)
{
if(acc2.Activity_Email__c != null && acc2.Activity_Email__c != '' && acc2.Status__c == true )
{
set<string> emailids = new set<string>();
list<string> templist = acc2.Activity_Email__c.split(',');
emailids.addALL(templist);
AccRecp.put(acc2.id,emailids);
}
}
if(!AccRecp.isEmpty())
{
EmailTemplate et = [ Select Body, HtmlValue, Id, Name, Subject from EmailTemplate where Name='Post Suspension' Limit 1];
List<Messaging.SingleEmailMessage> theEmails = new list<Messaging.SingleEmailMessage>();
for(Account ac: newList2)
{
if(AccRecp.containsKey(ac.id))
{
set<string> temp = AccRecp.get(ac.Id);
list<string> emaillist= new list<string>();
emaillist.add(label.Account_team);
emaillist.addAll(temp);
string body = et.Body;
OrgWideEmailAddress[] ar = [select Id,Address from OrgWideEmailAddress where Address = 'abc@company.com' ];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emaillist);
mail.setSubject(' Notice of Suspension-' +' '+ ac.Name);
mail.setOrgWideEmailAddressId(ar.get(0).id);
mail.setPlainTextBody(body);
theEmails.add(mail);
}
}
if(!theEmails.isEmpty())
{
list<Messaging.Email> allMails = new list<Messaging.Email>();
for( Integer j = 0; j < theEmails.size(); j++ ){
allMails.add(theEmails.get(j));
}
if(!allMails.isempty())
{
Messaging.SendEmailResult[] results = Messaging.sendEmail( allMails,false );
if (results[0].isSuccess()) {
system.debug('The email was sent successfully.');
} else {
system.debug('The email failed to send: '+results[0].getErrors());
}
}
}
}
}
I,m unable to get code coverage for sending emailnotification. can anyone help me. Thanks in advance
public class NotificationEmail {
public static void sendNotificationEmail(List<Account> newList2){
map<id,set<string>> AccRecp = new map<id,set<string>>();
for(Account acc2 : newList2)
{
if(acc2.Activity_Email__c != null && acc2.Activity_Email__c != '' && acc2.Status__c == true )
{
set<string> emailids = new set<string>();
list<string> templist = acc2.Activity_Email__c.split(',');
emailids.addALL(templist);
AccRecp.put(acc2.id,emailids);
}
}
if(!AccRecp.isEmpty())
{
EmailTemplate et = [ Select Body, HtmlValue, Id, Name, Subject from EmailTemplate where Name='Post Suspension' Limit 1];
List<Messaging.SingleEmailMessage> theEmails = new list<Messaging.SingleEmailMessage>();
for(Account ac: newList2)
{
if(AccRecp.containsKey(ac.id))
{
set<string> temp = AccRecp.get(ac.Id);
list<string> emaillist= new list<string>();
emaillist.add(label.Account_team);
emaillist.addAll(temp);
string body = et.Body;
OrgWideEmailAddress[] ar = [select Id,Address from OrgWideEmailAddress where Address = 'abc@company.com' ];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emaillist);
mail.setSubject(' Notice of Suspension-' +' '+ ac.Name);
mail.setOrgWideEmailAddressId(ar.get(0).id);
mail.setPlainTextBody(body);
theEmails.add(mail);
}
}
if(!theEmails.isEmpty())
{
list<Messaging.Email> allMails = new list<Messaging.Email>();
for( Integer j = 0; j < theEmails.size(); j++ ){
allMails.add(theEmails.get(j));
}
if(!allMails.isempty())
{
Messaging.SendEmailResult[] results = Messaging.sendEmail( allMails,false );
if (results[0].isSuccess()) {
system.debug('The email was sent successfully.');
} else {
system.debug('The email failed to send: '+results[0].getErrors());
}
}
}
}
}
Hi Manj_SFDC,
My test class code:
public class AccountNotificationEmailTest {
Public static testMethod void sendingmailTest() {
Test.startTest();
Account a1 = new Account();
a1.Name = 'test';
a1.Type = 'customer';
a1.Activity_Email__c = 'test@gmail.com';
a1.Channel__c = 'Direct';
insert a1;
string accId = a1.id;
Account accountRecord = [select id,Name,Status__c,Activity_Email__c from Account where id=: accId];
accountRecord.Status__c = true ;
accountRecord.Activity_Email__c = 'test@gmail2.com';
update accountRecord;
Test.stopTest();
}
}
My trigger for the Apex class NotificationEmail:
trigger sendNotificationtrigger on Account (after insert,after update) {
if ( Trigger.isAfter )
{
if ( Trigger.isInsert || Trigger.isUpdate )
{
List<Account> aclist = new list<Account>();
for(Account ac :trigger.new)
{
if( ac.Status__c == true && Trigger.oldmap.get(ac.id).Status__c != ac.Status__c)
aclist.add(ac);
}
if(!aclist.isempty())
NotificationEmail.sendNotificationEmail(aclist);
}
}
}
The thing is in trigger NotificationEmail.sendNotificationEmail(aclist); is not covering(meanig my apex class method is not covering
),. Please help me where i'm making mistake
Manj_SFDC
aclist.add(ac); line is not covered.
Manj_SFDC
did you change it if( ac.Status__c && Trigger.oldmap.get(ac.id).Status__c != ac.Status__c)
Yes, i have changed.
a1.Name = 'test';
a1.Type = 'customer';
a1.Activity_Email__c = 'test@gmail.com';
a1.Channel__c = 'Direct';
a1.Status__c = false;
insert a1;
use this and try
Account a1 = new Account();
a1.Name = 'test';
a1.Type = 'customer';
a1.Activity_Email__c = 'test@gmail.com';
a1.Channel__c = 'Direct';
a1.Status__c = false;
insert a1;
I have tried this but asusal code coverage is not happening. can you help me out.