function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jpbenjpben 

Task Email Trigger

Hello,

 

I have the following code that is supposed to send an email to the Task Creator once the Task has been Completed.  I am getting an error .... "System.NullPointerException: Attempt to de-reference a null object:  Trigger.TaskSendEmail: line 18, column 1."  I cant figure out what I need to change on the code?    I highlighted in red; line 18.   Thanks in advance

 

 

trigger TaskSendEmail on Task (after 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)
    {
        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 Closed');    // 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 = 'Hello {0}, \nYour task has been modified. Here are the details - \n\n';
        template+= 'Subject - {1}\n';
        template+= 'Due Date - {2}\n';
        String duedate = '';
        
        if (tsk.ActivityDate==null)
            duedate = '';
        else
            duedate = tsk.ActivityDate.format();
            
        List<String> args = new List<String>();
        args.add(theUser.Name);
        args.add(tsk.Subject);
        args.add(duedate);
        
       
        // Here's the String.format() call.
        String formattedHtml = String.format(template, args);
       
        mail.setPlainTextBody(formattedHtml);
        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger TaskSendEmail on Task (after 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]);// Creating map for users who satisfied the conditions
    for(Task tsk : Trigger.New)
    {
        if(tsk.RecordTypeId == '012Z00000004WFV' && tsk.Status == 'Completed')
        {
        	User theUser = userMap.get(tsk.CreatedById);  // you will not have the user records for all the tasks createdbyIds
        
	        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 Closed');    // 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 = 'Hello {0}, \nYour task has been modified. Here are the details - \n\n';
	        template+= 'Subject - {1}\n';
	        template+= 'Due Date - {2}\n';
	        String duedate = '';
	        
	        if (tsk.ActivityDate==null)
	            duedate = '';
	        else
	            duedate = tsk.ActivityDate.format();
	            
	        List<String> args = new List<String>();
	        args.add(theUser.Name);
	        args.add(tsk.Subject);
	        args.add(duedate);
	        
	       
	        // Here's the String.format() call.
	        String formattedHtml = String.format(template, args);
	       
	        mail.setPlainTextBody(formattedHtml);
	        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
	    }
    }
}

 

All Answers

Naidu PothiniNaidu Pothini
trigger TaskSendEmail on Task (after 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]);// Creating map for users who satisfied the conditions
    for(Task tsk : Trigger.New)
    {
        if(tsk.RecordTypeId == '012Z00000004WFV' && tsk.Status == 'Completed')
        {
        	User theUser = userMap.get(tsk.CreatedById);  // you will not have the user records for all the tasks createdbyIds
        
	        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 Closed');    // 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 = 'Hello {0}, \nYour task has been modified. Here are the details - \n\n';
	        template+= 'Subject - {1}\n';
	        template+= 'Due Date - {2}\n';
	        String duedate = '';
	        
	        if (tsk.ActivityDate==null)
	            duedate = '';
	        else
	            duedate = tsk.ActivityDate.format();
	            
	        List<String> args = new List<String>();
	        args.add(theUser.Name);
	        args.add(tsk.Subject);
	        args.add(duedate);
	        
	       
	        // Here's the String.format() call.
	        String formattedHtml = String.format(template, args);
	       
	        mail.setPlainTextBody(formattedHtml);
	        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
	    }
    }
}

 

This was selected as the best answer
SurpriseSurprise

I tried your trigger in my org and it is working fine.I am not getting any exception