You need to sign in to do that
Don't have an account?
Test Class - Task Email Trigger
I finally got my code to work and now I need help with my Test Class. My Test Class is only giving me 24% code coverage. I am not sure how to test the bottom portion of my trigger. Please advice.
Here is my trigger:
trigger TaskSendEmail on Task (before update) {
// Don't forget this- all triggers in SF are bulk triggers and so
// they can fire on multiple objects. So you need to process objects
// in a FOR loop.
Set<Id> CBIds = new Set<Id>();
for(Task tsk: Trigger.New)
if(tsk.RecordTypeId == '012Z00000004WFV' && tsk.Status == 'Completed')
CBIds.add(tsk.CreatedById);
// Build a map of all users who created the tasks.
Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :CBIds]);
for(Task tsk : Trigger.New){
if(!TriggerHelperClass.HasAlreadyFired()){
if(tsk.RecordTypeId == '012Z00000004WFV' && tsk.Status == 'Completed')
{
User theUser = userMap.get(tsk.CreatedById);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {theUser.Email};
mail.setToAddresses(toAddresses); // Set the TO addresses
mail.setSubject('The following Task has been Completed'); // Set the subject
// Next, create a string template. Specify {0}, {1} etc. in place of actual values.
// You can replace these values with a call to String.Format.
String template = 'The following Task has been Completed: \n\n';
template+= 'Subject: {0}\n';
template+= 'Comments: {1}\n\n';
template+= 'Click the link to access the record: https://cs11.salesforce.com/{2}\n';
String duedate = '';
if (tsk.ActivityDate==null)
duedate = '';
else
duedate = tsk.ActivityDate.format();
List<String> args = new List<String>();
args.add(tsk.Subject);
args.add(tsk.Description);
args.add(tsk.Id);
// Here's the String.format() call.
String formattedHtml = String.format(template, args);
mail.setPlainTextBody(formattedHtml);
TriggerHelperClass.setAlreadyFired();
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}
}
Here is my Test Class:
@isTest (seeAllData=true)
public class TestTaskSendEmail {
static testMethod void myUnitTest(){
Test.startTest();
Task testTask = new Task ();
testTask.OwnerId='005G0000001jWxo';
testTask.Type='Email';
testTask.Subject='Test';
testTask.ActivityDate=system.Today();
testTask.Priority='Normal';
testTask.Status='Not Started';
insert testTask;
testTask.Status ='Completed';
update testTask;
System.assertEquals (testTask.Status,'Completed');
Test.stopTest();
}
}
Hi,
In your test class add record type to the task that will do it. See the below code.
I have added "testTask.RecordTypeId = '012Z00000004WFV';" before insert the task rec.
Regards,
All Answers
Hi,
In your test class add record type to the task that will do it. See the below code.
I have added "testTask.RecordTypeId = '012Z00000004WFV';" before insert the task rec.
Regards,