You need to sign in to do that
Don't have an account?
RajaMohan
How to cover the test coverage?
Hi Folks,
I am unable to cover the parts which is marked in red in test coverage. Please help me in covering these parts. It is very urgernt.
public PageReference EmailQuote(){
try
{
PageReference pcd = Page.esc_Quote;
pcd.getParameters().put('id',Oppr.id);
Blob pdfBlob = pcd.getContent();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String [] toAddresses = new String[] {SearchTo};
String [] toCC = new String[] {SearchCC};
String [] toBCC = new String[] {SearchBCC};
str1=SearchBody.replaceAll('\n','<br/>');
if(Acc.RecordTypeId=='0122000000058UW' || Acc.RecordTypeId=='01220000000HS25'){
Contact cc=[select id,name,Email from contact where id=:oppr.contact__c];
if(cc.Email != null){
email.setTargetObjectId(cc.Id);
email.setSubject(SearchSubject);
email.setHtmlBody(str1);
if(SearchCC!=''){
email.setCcAddresses(toCC);
}
if(SearchBCC!=''){
email.setBccAddresses(toBCC);
}
email.setSaveAsActivity(true);
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('ES_'+ Oppr.Quote_Number__c+'-'+ Oppr.Quote_Version__c + '.pdf');
efa.setBody(pdfBlob); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
//Create an event.
Task myTask = new Task();
myTask.WhatId = Oppr.Id;
myTask.WhoId = cc.Id;
myTask.IsReminderSet = true;
myTask.ReminderDateTime = System.Today();
myTask.ActivityDate = System.Today();
myTask.Description = SearchBody;
myTask.Subject = SearchSubject;
myTask.OwnerId = Usr.Id;
myTask.Status = 'Completed';
Insert myTask;
}
else{
email.setSaveAsActivity(false);
email.setTargetObjectId(usr.Id);
email.setSubject('No Email to send the Quotation');
email.setHtmlBody('Dear '+usr.Name+', No Email ID found into the Contact to send the Quotation.');
}
}
if(Acc.RecordTypeId=='0122000000058cV'){
//email.setTargetObjectId(Con.Id);
email.setSubject(SearchSubject);
email.setHtmlBody(str1);
email.setToAddresses(toAddresses);
if(SearchCC!=''){
email.setCcAddresses(toCC);
}
if(SearchBCC!=''){
email.setBccAddresses(toBCC);
}
email.setSaveAsActivity(true);
//Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('ES_'+ Oppr.Quote_Number__c+'-'+ Oppr.Quote_Version__c + '.pdf');
efa.setBody(pdfBlob); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
//Create an event.
Task myTask = new Task();
myTask.WhatId = Oppr.Id;
myTask.WhoId = con.Id;
myTask.IsReminderSet = true;
myTask.ReminderDateTime = System.Today();
myTask.ActivityDate = System.Today();
myTask.Description = SearchBody;
myTask.Subject = SearchSubject;
myTask.OwnerId = Usr.Id;
myTask.Status = 'Completed';
Insert myTask;
}
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Pagereference OppPage = new ApexPages.StandardController(Oppr).view();
opppage.setredirect(true);
return OppPage;
}catch(Exception e){
System.debug('ERROR:' + e);
return null;
}
}
Hi Raja.
This code I guess is not being covered since you are hardcoding a reference to specific Ids. Preferrably you would call for the RecordType ids by name and use the retrieved ids instead, then you won't get a conflict between Ids in your development and production orgs.
Something like:
The other solution would be to use a test condition to trigger the execution of the code. You would create a boolean variable in your class named something like "isTest", which default to false but you'd set to true in your test methods. Then you'd change your if conditions to this:
That should also trigger the execution of the code block in question.
Hope this helps.
Regards,
Ivar
Hi Ivar
Thanks for your reply. The test coverage is still the same. This is my full code.
Thanks
Raj
Hi again.
I don't see your test methods in there. Are you sure they work with accounts of all the record types mentioned, and thus enforce the execution of those code blocks?
Regards,
Ivar
It seems to me that you are erroring and it is causing you to fall out of the function.
Error seems to occur here. You don't want your try/catch block to be over all that code. It is meant for a specific spot that you think could error, and so that you can take the right actions if it does.
can you post your test methods? I'm a big fan of test suites vs. test cases which test your application more like how you actually use the app. Once you post the test methods I can give some pointers
This is test class for my Class.
thanks