+ Start a Discussion
Ramk123Ramk123 

apex code coverage help

Task completion email notification to task creator and Should not generate a notification to yourself when you complete the task.

trigger Taskcompletednotification on Task (after update) {
   List<Messaging.SingleEmailMessage> mails= new List<Messaging.SingleEmailMessage>(); 
   Map<Id,Id> userTaskIds = new Map<Id,Id>();
   EmailTemplate Template =  [SELECT Id,Name FROM EmailTemplate WHERE Name='Task creator email notification'];
   for(Task tsk : Trigger.New)
   {
        if(tsk.Status == 'Completed'&& tsk.OwnerId != tsk.CreatedById)
        {
           userTaskIds.put(tsk.CreatedById,tsk.Id);
        } 
   }
   
   for(User targetId : [SELECT id,Email From User where Id IN :userTaskIds.keySet()])
   {
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
         email.setSaveAsActivity(false);
         email.setTargetObjectId(targetId.Id); 
         email.setTemplateId(Template.Id);  
         email.setWhatId(userTaskIds.get(targetId.id));
         mails.add(email);
   }
 
   if(mails.size() > 0)
   {  
      try
      {
         System.debug('Control Came Here');
         Messaging.sendEmail(mails);
      }catch(Exception e)
      {
         System.debug('Error Message :'+e);
      }  
   }                
         
}

Test Class
========
@isTest
public class Taskcompletednotificationtest{

@isTest
public static void taskUpdateTest()
{
    Task tsk = new Task(); 
    tsk.subject='test';
    tsk.ActivityDate=Date.Today();
    tsk.Status='Not Started';
    tsk.Priority='Normal';
    tsk.OwnerId='00524000003MWO2';
    tsk.CreatedById='00524000003MWO2';
    insert tsk;
    
    Task task = [SELECT status FROM Task WHERE id=:tsk.id];
    task.status = 'Completed';
    update task;
}

}
Best Answer chosen by Ramk123
Amit Chaudhary 8Amit Chaudhary 8
Please update your test class like below
@isTest
public class Taskcompletednotificationtest
{
	@isTest
	public static void taskUpdateTest()
	{


        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
		
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
		insert u;
		
		
		Task tsk = new Task(); 
		tsk.subject='test';
		tsk.ActivityDate=Date.Today();
		tsk.Status='Not Started';
		tsk.Priority='Normal';
		tsk.OwnerId = u.id;
		insert tsk;
		
		
		tsk.status = 'Completed';
		update task;
		
	}

}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your test class like below
@isTest
public class Taskcompletednotificationtest
{
	@isTest
	public static void taskUpdateTest()
	{


        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
		
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
		insert u;
		
		
		Task tsk = new Task(); 
		tsk.subject='test';
		tsk.ActivityDate=Date.Today();
		tsk.Status='Not Started';
		tsk.Priority='Normal';
		tsk.OwnerId = u.id;
		insert tsk;
		
		
		tsk.status = 'Completed';
		update task;
		
	}

}

Let us know if this will help you
This was selected as the best answer
Ramk123Ramk123
Thank a lot, now code coverage is 94%