You need to sign in to do that
Don't have an account?
Aloke Singh 6
I can't able to understand why code coverage is showing only 53% for below mentioned class
I can't able to understand why code coverage is showing only 53% for below mentioned class
Class :-
public class PIREmailNotifcation {
final static string className = 'PIREmailNotifcation';
public static void MasterCaseEmail(list<case>caselist){
try{
for (case caserecord:caselist){
if( caserecord.Master_Case__c==True && caserecord.RFO_Available__c==True){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'customerservicesupport-uat@mitel.com'];
system.debug('@@@@'+owea);
// string templatelabel = Label.PIRExternalEmailTemplate;
// Map<String,PIRInternalEMailIds__c> allemail = PIRInternalEMailIds__c.getAll();
//PIRInternalEMailIds__c cs = PIRInternalEMailIds__c.getAll();
String[] toAddresses = new String[] {(caserecord.ContactEmail)};
mail.setToAddresses(toAddresses);
mail.whatid=caserecord.Id;
mail.setTargetObjectId(caserecord.ContactId);
mail.setReplyTo('aloke.singh90@gmail.com');
mail.setUseSignature(false);
mail.setTemplateId('00XC0000001R6IW');
mail.saveAsActivity = True;
mail.setOrgWideEmailAddressId(owea.get(0).Id);
PIR__c[] pirid=[select Id,MasterCase_ID__c from PIR__c where MasterCase_ID__c =: caserecord.Id];
system.debug('%%%%pirid%%%'+pirid);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for(Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :pirid.get(0).Id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
catch(exception me){HandlerUtility.trackException(me, className);
}
}
public static void childCaseEMail(list<case>childcase){
try{
//if(pirtriggerboolean.runOnce()){
// pirtriggerboolean.iteration= false;
set<Id> caseId = new set<Id>();
set<Id> contactId = new set<Id>();
for(case caserecord:childcase){
if(caserecord.CaseNumber != null ){
caseId.add(caserecord.Id);
}
}
//system.debug('$$$$$'+childcaseId);
Case[] childcaseId = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId,
SuppliedEmail FROM Case WHERE ParentId=:caseId];
system.debug('$$$$$'+childcaseId);
Map<Id,String> ContactEmail = new Map<Id, String>();
Map<Id, String> ContactFirstname = new Map<Id, String>();
for(case c:childcaseId){
contactId.add(c.ContactId);
}
list<contact> conList=[select id, email from Contact where id IN :contactId];
system.debug('$$$$$'+conList);
if(conList.size() !=null){
for(Contact c2:conList)
ContactEmail.put(c2.id,c2.email);
}
for(case childrecord :childcaseId){
for(contact cont:conList){
if(childrecord.ContactId == cont.Id ){
//string templatelabel = Label.PIRExternalEmailTemplate;
PIR__c [] pirrecordId=[select Id,MasterCase_ID__c from PIR__c where MasterCase_ID__c IN: caseId];
system.debug('$$$$$'+pirrecordId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'customerservicesupport-uat@mitel.com'];
system.debug('@@@@'+owea);
String[] toAddresses = new String[] {ContactEmail.get(cont.id)};
mail.setToAddresses(toAddresses);
mail.whatid=childrecord.Id;
mail.setTargetObjectId(childrecord.ContactId);
mail.setReplyTo('aloke.singh90@gmail.com');
mail.setUseSignature(false);
mail.setTemplateId('00X4F000000gs6W');
mail.saveAsActivity = True;
mail.setOrgWideEmailAddressId(owea.get(0).Id);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for(Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :pirrecordId.get(0).Id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
// }
}
catch (exception me){HandlerUtility.trackException(me, className);
}
}
}
Test Class :-
@isTest
public class PIREmailNotifcationTest {
static{
TestDataUtility.utilityTestData();
}
static testmethod void testemailsent(){
Contact con = new Contact();
con.firstname = 'test';
con.lastname = 'test';
con.email = 'test@example.com';
insert con;
Case caserecord = TestDataUtility.CreateTestCase();
caserecord.Master_Case__c =TRUE;
caserecord.RFO_Available__c =TRUE;
caserecord.contactid = con.id;
insert caserecord;
Case ccase = TestDataUtility.createtestcase();
ccase.parentid = caserecord.id;
ccase.contactid = con.id;
PIR__c pirrecord = new PIR__c();
pirrecord.Name = '12345';
pirrecord.CurrencyIsoCode='USD';
pirrecord.MasterCase_ID__c=caserecord.Id;
insert pirrecord;
Attachment myAttachment = new Attachment();
myAttachment.Body= Blob.valueOf('Test Attachment Body');
myAttachment.ParentId= pirrecord.Id;
myAttachment.Name='ABC';
insert myAttachment;
system.debug ('BodyLength::'+ myattachment.BodyLength);
caserecord.Master_Case__c =TRUE;
caserecord.RFO_Available__c =TRUE;
update caserecord;
/* List<Attachment> att = [Select id from attachment where parentid =: pirrecord.id];
System.assertEquals(1, att.size());
EmailMessage outGoingMail= new EmailMessage();
outGoingMail.fromaddress='test@test.com';
outGoingMail.toAddress = 'con1.Email';
outGoingMail.subject = 'Opt Out Test Message';
outGoingMail.TextBody= 'This is the message body BR-Interno.';
outGOingMail.ParentID = caserecord.id;
insert outGoingMail; */
}
}
Class :-
public class PIREmailNotifcation {
final static string className = 'PIREmailNotifcation';
public static void MasterCaseEmail(list<case>caselist){
try{
for (case caserecord:caselist){
if( caserecord.Master_Case__c==True && caserecord.RFO_Available__c==True){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'customerservicesupport-uat@mitel.com'];
system.debug('@@@@'+owea);
// string templatelabel = Label.PIRExternalEmailTemplate;
// Map<String,PIRInternalEMailIds__c> allemail = PIRInternalEMailIds__c.getAll();
//PIRInternalEMailIds__c cs = PIRInternalEMailIds__c.getAll();
String[] toAddresses = new String[] {(caserecord.ContactEmail)};
mail.setToAddresses(toAddresses);
mail.whatid=caserecord.Id;
mail.setTargetObjectId(caserecord.ContactId);
mail.setReplyTo('aloke.singh90@gmail.com');
mail.setUseSignature(false);
mail.setTemplateId('00XC0000001R6IW');
mail.saveAsActivity = True;
mail.setOrgWideEmailAddressId(owea.get(0).Id);
PIR__c[] pirid=[select Id,MasterCase_ID__c from PIR__c where MasterCase_ID__c =: caserecord.Id];
system.debug('%%%%pirid%%%'+pirid);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for(Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :pirid.get(0).Id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
catch(exception me){HandlerUtility.trackException(me, className);
}
}
public static void childCaseEMail(list<case>childcase){
try{
//if(pirtriggerboolean.runOnce()){
// pirtriggerboolean.iteration= false;
set<Id> caseId = new set<Id>();
set<Id> contactId = new set<Id>();
for(case caserecord:childcase){
if(caserecord.CaseNumber != null ){
caseId.add(caserecord.Id);
}
}
//system.debug('$$$$$'+childcaseId);
Case[] childcaseId = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId,
SuppliedEmail FROM Case WHERE ParentId=:caseId];
system.debug('$$$$$'+childcaseId);
Map<Id,String> ContactEmail = new Map<Id, String>();
Map<Id, String> ContactFirstname = new Map<Id, String>();
for(case c:childcaseId){
contactId.add(c.ContactId);
}
list<contact> conList=[select id, email from Contact where id IN :contactId];
system.debug('$$$$$'+conList);
if(conList.size() !=null){
for(Contact c2:conList)
ContactEmail.put(c2.id,c2.email);
}
for(case childrecord :childcaseId){
for(contact cont:conList){
if(childrecord.ContactId == cont.Id ){
//string templatelabel = Label.PIRExternalEmailTemplate;
PIR__c [] pirrecordId=[select Id,MasterCase_ID__c from PIR__c where MasterCase_ID__c IN: caseId];
system.debug('$$$$$'+pirrecordId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'customerservicesupport-uat@mitel.com'];
system.debug('@@@@'+owea);
String[] toAddresses = new String[] {ContactEmail.get(cont.id)};
mail.setToAddresses(toAddresses);
mail.whatid=childrecord.Id;
mail.setTargetObjectId(childrecord.ContactId);
mail.setReplyTo('aloke.singh90@gmail.com');
mail.setUseSignature(false);
mail.setTemplateId('00X4F000000gs6W');
mail.saveAsActivity = True;
mail.setOrgWideEmailAddressId(owea.get(0).Id);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for(Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :pirrecordId.get(0).Id]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}
mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
// }
}
catch (exception me){HandlerUtility.trackException(me, className);
}
}
}
Test Class :-
@isTest
public class PIREmailNotifcationTest {
static{
TestDataUtility.utilityTestData();
}
static testmethod void testemailsent(){
Contact con = new Contact();
con.firstname = 'test';
con.lastname = 'test';
con.email = 'test@example.com';
insert con;
Case caserecord = TestDataUtility.CreateTestCase();
caserecord.Master_Case__c =TRUE;
caserecord.RFO_Available__c =TRUE;
caserecord.contactid = con.id;
insert caserecord;
Case ccase = TestDataUtility.createtestcase();
ccase.parentid = caserecord.id;
ccase.contactid = con.id;
PIR__c pirrecord = new PIR__c();
pirrecord.Name = '12345';
pirrecord.CurrencyIsoCode='USD';
pirrecord.MasterCase_ID__c=caserecord.Id;
insert pirrecord;
Attachment myAttachment = new Attachment();
myAttachment.Body= Blob.valueOf('Test Attachment Body');
myAttachment.ParentId= pirrecord.Id;
myAttachment.Name='ABC';
insert myAttachment;
system.debug ('BodyLength::'+ myattachment.BodyLength);
caserecord.Master_Case__c =TRUE;
caserecord.RFO_Available__c =TRUE;
update caserecord;
/* List<Attachment> att = [Select id from attachment where parentid =: pirrecord.id];
System.assertEquals(1, att.size());
EmailMessage outGoingMail= new EmailMessage();
outGoingMail.fromaddress='test@test.com';
outGoingMail.toAddress = 'con1.Email';
outGoingMail.subject = 'Opt Out Test Message';
outGoingMail.TextBody= 'This is the message body BR-Interno.';
outGOingMail.ParentID = caserecord.id;
insert outGoingMail; */
}
}
Some other thoughts:
1. when posting code, use the code insert button - it makes the code easier to read
2. When doing test methods, it is recommended that you do individual tests for each methods. Potentially also separate Insert and Update tests. It makes trouble shooting so much easier.
Regards
Andrew
Thanks for your suggestion and noted. Basically I facing difficulty to manage Messaging.SingleEmailMessage class to write.
Can you help me to improve this code coverage at least and it's urgent so that's why seeking some help
Regards
Andrew