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
Debendra Ray 8Debendra Ray 8 

INVALID_EMAIL_ADDRESS Error while invoking common.apex.methods.MessagingStaticMethods$EmailExecutionException: SendEmail

Dear All,
I'm getting the following error while invoking SendEmail method :
16:00:18:192 VARIABLE_ASSIGNMENT [11]|e|"common.apex.methods.MessagingStaticMethods$EmailExecutionException: SendEmail failed. First exception on row 15; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []"|0x989add4
Appreciate, if you could provide an early resolution of this problem.

Please find the code appended herewith
global class SendReminderForTaskCompletion implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
      
       try{
            List<Task>  lstTask = [Select Id,Subject,Priority,Status,ActivityDate,Description,OwnerId, TAT__c from Task where Status != 'Completed' ];                 
                                                         
            sendEmailToTaskOwner(lstTask); 
           
       }catch(Exception e)
       {
         System.debug('Error : SendReminderForTaskCompletion->'+ e.getStackTraceString());  
       }
         
   }  
    
    // This method will send Email To Manager automatically
    public void sendEmailToTaskOwner(List<Task> lstTask ){
                
        List<Messaging.SingleEmailMessage> lstMail = new List<Messaging.SingleEmailMessage>();
        
        List<String> ownerId = new List<String>();
        for(Task Tsk: lstTask){
            ownerId.add(Tsk.OwnerId);
        }
       
        List<User> userDetails = [select Id, Name, Email, ManagerId, Manager.Name, Manager.Email from User where Id IN :ownerId];       
                
        List<String> ownerDtlsArray = new List<String>();
        for(User u:userDetails){
            ownerDtlsArray.add(u.Id );
            ownerDtlsArray.add(u.Name );
            ownerDtlsArray.add(u.Email );
            ownerDtlsArray.add(u.ManagerId );
            ownerDtlsArray.add(u.Manager.Name );
            ownerDtlsArray.add(u.Manager.Email );
        }
                      
                    
        
        for(Integer i=0; i< lstTask.size(); i++ ){
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();            
            String opprOwnerEmail='';
            String opprOwnerName='';
            String opprOwnerManagerEmail='';
            String opprOwnerManagerName='';
            for(Integer j=0; j< ownerDtlsArray.size(); j=j+6){
             
                if( (lstTask[i].OwnerId).equals(ownerDtlsArray[j]) ){
                    opprOwnerName = ownerDtlsArray[j+1];
                    opprOwnerEmail = ownerDtlsArray[j+2];
                    opprOwnerManagerName = ownerDtlsArray[j+4];
                    opprOwnerManagerEmail = ownerDtlsArray[j+5];
                    break;
                }
            }
             
            String[] toAddresses = new String[] {opprOwnerEmail};
            String[] ccAddresses = new String[] {opprOwnerManagerEmail};   
            System.debug('DEBUG :: SendReminderForTaskCompletion:: toAddresses :: ->'+ toAddresses);
            System.debug('DEBUG :: SendReminderForTaskCompletion:: ccAddresses :: ->'+ ccAddresses);
            if( (toAddresses.size() >0 & toAddresses != null) & (ccAddresses.size() >0 & ccAddresses != null) ){
                mail.setToAddresses(toAddresses);
                mail.setCcAddresses(ccAddresses);                              
                mail.setSubject('Your Pending Tasks ' );
                mail.setHtmlBody('<br/>Due Date of the task <b> :' +lstTask[i].ActivityDate+'</b><br/>'+'Priority of the task : <b>' + lstTask[i].Priority + '</b><br/> Subject : <b>' +  lstTask[i].Subject  + '</b>'+'</b><br/> Description : <b>' +  lstTask[i].Description +'</b>'+'</b><br/> Status : <b>' +  lstTask[i].Status  + '</b>'+'</b><br/> TAT : <b>' +  lstTask[i].TAT__c+' <br/><br/><br/>Thanks & Regards <br/> <br/>Salesforce Administrator');
                //mail.setSaveAsActivity(true); 
                System.debug('DEBUG :: SendReminderForTaskCompletion:: mail :: ->'+ mail);
                lstMail.add(mail);
            }
             
        } 
        System.debug('DEBUG : SendReminderForTaskCompletion:: lstMail :: ->'+ lstMail); 
        if(lstMail.size() >0){
           Messaging.sendEmail(lstMail);   
        }
        
       
    }


}



 
Neetu_BansalNeetu_Bansal
Hi Debendra,

I guess due to some reasons, your opprOwnerEmail is null, that's why you are getting the exception. Please check that once or put null pointer checks.

Let me know if you need any other help.

Thanks,
Neetu
Debendra Ray 8Debendra Ray 8
Dear Neetu,

I've put necessary Null Pointer Checks before adding it to the mail object in the code - However, the issue still persists.
Please could you suggest.

Thanks.

Regards,

Debendra
 
Neetu_BansalNeetu_Bansal
Hi Debendra,

In your code at line no 63, you are checking that size > 0 and not null, but the list contains a null string, so its size will always be more than 0 and it cannot be null. You need to put checks before adding into list at line no. 59 like:
String[] toAddresses;
if( opprOwnerEmail != null && opprOwnerEmail != '' )
{
	toAddresses = new String[] {opprOwnerEmail};
}
Let me know, if you need any other help.

Thanks,
Neetu
gayatri sfdcgayatri sfdc
Hi Debendra,

Only "simple fields" are available in the trigger out of the box. In this case it means you cant get this value  (u.Manager.Email )  @line no : 36
so, the ownerDtlsArray is coming null. please check.

Keep Learning :)

Thanks
Gayatri
Debendra Ray 8Debendra Ray 8
Dear Neetu,

After applying the changes you've suggested , I'm getting the following error :

|EXCEPTION_THROWN|[81]|System.EmailException: SendEmail failed. First exception on row 15; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc):

Please could you suggest asap.

Thanks very much.

Regards,

Debendra Ray
Neetu_BansalNeetu_Bansal
Hi Debendra,

You need to decide to whom you want to send the email because your opprOwnerEmail is null and you are setting this as To address, now your code will not allow to send any attribute because toAddresses.size() is 0.
if( (toAddresses.size() >0 & toAddresses != null) & (ccAddresses.size() >0 & ccAddresses != null) )
Now, you are not able to set any attribute of sending mail.

Thanks,
Neetu
Debendra Ray 8Debendra Ray 8
Dear Neetu,

Please find my ammended code , which is throwing the above error :
global class SendReminderForTaskCompletion implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
      
       try{
            List<Task>  lstTask = [Select Id,Subject,Priority,Status,ActivityDate,Description,OwnerId, TAT__c from Task where Status != 'Completed' ];                 
                                                         
            sendEmailToTaskOwner(lstTask);
           
       }catch(Exception e)
       {
         System.debug('Error : SendReminderForTaskCompletion->'+ e.getStackTraceString());  
       }
         
   }  
    
    // This method will send Email To Manager automatically
    public void sendEmailToTaskOwner(List<Task> lstTask ){
                
        List<Messaging.SingleEmailMessage> lstMail = new List<Messaging.SingleEmailMessage>();
        
        List<String> ownerId = new List<String>();
        for(Task Tsk: lstTask){
            ownerId.add(Tsk.OwnerId);
        }
       
        List<User> userDetails = [select Id, Name, Email, ManagerId, Manager.Name, Manager.Email from User where Id IN :ownerId];       
                
        List<String> ownerDtlsArray = new List<String>();
        for(User u:userDetails){
            ownerDtlsArray.add(u.Id );
            ownerDtlsArray.add(u.Name );
            ownerDtlsArray.add(u.Email );
            ownerDtlsArray.add(u.ManagerId );
            ownerDtlsArray.add(u.Manager.Name );
            ownerDtlsArray.add(u.Manager.Email );
        }
                      
                    
        
        for(Integer i=0; i< lstTask.size(); i++ ){
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();            
            String opprOwnerEmail='';
            String opprOwnerName='';
            String opprOwnerManagerEmail='';
            String opprOwnerManagerName='';
            for(Integer j=0; j< ownerDtlsArray.size(); j=j+6){
             
                if( (lstTask[i].OwnerId).equals(ownerDtlsArray[j]) ){
                    opprOwnerName = ownerDtlsArray[j+1];
                    opprOwnerEmail = ownerDtlsArray[j+2];
                    opprOwnerManagerName  = ownerDtlsArray[j+4];
                    opprOwnerManagerEmail = ownerDtlsArray[j+5];
                    break;
                }
            }
            
            String[] toAddresses = new String[]{};
            String[] ccAddresses = new String[]{};    
                
            if(opprOwnerEmail.trim() !='' && opprOwnerEmail != null && opprOwnerManagerEmail != null && opprOwnerManagerEmail.trim() !='' ) {
                 toAddresses = new String[] {opprOwnerEmail};
                 ccAddresses = new String[] {opprOwnerManagerEmail};
            }
            System.debug('DEBUG :: SendReminderForTaskCompletion:: toAddresses :: ->'+ toAddresses);
            System.debug('DEBUG :: SendReminderForTaskCompletion:: ccAddresses :: ->'+ ccAddresses);
            
                mail.setToAddresses(toAddresses);
                
                mail.setCcAddresses(ccAddresses);                              
                mail.setSubject('Your Pending Tasks ' );
                mail.setHtmlBody('<br/>Due Date of the task <b> :' +lstTask[i].ActivityDate+'</b><br/>'+'Priority of the task : <b>' + lstTask[i].Priority + '</b><br/> Subject : <b>' +  lstTask[i].Subject  + '</b>'+'</b><br/> Description : <b>' +  lstTask[i].Description +'</b>'+'</b><br/> Status : <b>' +  lstTask[i].Status  + '</b>'+'</b><br/> TAT : <b>' +  lstTask[i].TAT__c+' <br/><br/><br/>Thanks & Regards <br/> <br/>Salesforce Administrator');
                //mail.setSaveAsActivity(true);
                System.debug('DEBUG :: SendReminderForTaskCompletion:: mail :: ->'+ mail);
                lstMail.add(mail);
            
             
        }
        System.debug('DEBUG : SendReminderForTaskCompletion:: lstMail :: ->'+ lstMail);
       
        Messaging.sendEmail(lstMail);   
        
        
       
    }


}

Appreciate your further suggestions.

Thanks.

Regards,

Debendra
Neetu_BansalNeetu_Bansal
Hi Debendra,

Try this code:
global class SendReminderForTaskCompletion implements Schedulable
{
	global void execute(SchedulableContext ctx)
	{
		try
		{
            List<Task>  lstTask = [Select Id,Subject,Priority,Status,ActivityDate,Description,OwnerId, TAT__c from Task where Status != 'Completed' ];
            sendEmailToTaskOwner(lstTask);
		}
		catch(Exception e)
		{
			System.debug('Error : SendReminderForTaskCompletion->'+ e.getStackTraceString());  
		}
	}  
    
    // This method will send Email To Manager automatically
    public void sendEmailToTaskOwner( List<Task> lstTask )
	{
		List<Messaging.SingleEmailMessage> lstMail = new List<Messaging.SingleEmailMessage>();
        
        Set<Id> ownerId = new Set<Id>();
        for( Task Tsk: lstTask )
		{
            ownerId.add( Tsk.OwnerId );
        }
       
        Map<Id, User> userDetails = new Map<Id, User>([ Select Id, Name, Email, ManagerId, Manager.Name, Manager.Email from User Where Id IN :ownerId ]);
		
        for( Task t : lstTask )
		{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();            
            String opprOwnerEmail='';
            String opprOwnerName='';
            String opprOwnerManagerEmail='';
            String opprOwnerManagerName='';
            if( userDetails.containsKey( t.OwnerId ))
            {
                opprOwnerName = userDetails.get( t.OwnerId ).Name;
				opprOwnerEmail = userDetails.get( t.OwnerId ).Email;
				opprOwnerManagerName  = userDetails.get( t.OwnerId ).Manager.Name;
				opprOwnerManagerEmail = userDetails.get( t.OwnerId ).Manager.Email;
            }
            
            String[] toAddresses = new String[]{};
            String[] ccAddresses = new String[]{};    
                
            if( opprOwnerEmail != null && opprOwnerEmail.trim() != '' )
			{
                toAddresses = new String[]{ opprOwnerEmail };
            }
			
			if( opprOwnerManagerEmail != null && opprOwnerManagerEmail.trim() != '' )
			{
				ccAddresses = new String[] {opprOwnerManagerEmail};
			}
			
            System.debug('DEBUG :: SendReminderForTaskCompletion:: toAddresses :: ->'+ toAddresses);
            System.debug('DEBUG :: SendReminderForTaskCompletion:: ccAddresses :: ->'+ ccAddresses);
            
			mail.setToAddresses( toAddresses );
			mail.setCcAddresses( ccAddresses );                              
			mail.setSubject('Your Pending Tasks ' );
			mail.setHtmlBody('<br/>Due Date of the task <b> :' +t.ActivityDate+'</b><br/>'+'Priority of the task : <b>' + t.Priority + '</b><br/> Subject : <b>' +  t.Subject  + '</b>'+'</b><br/> Description : <b>' +  t.Description +'</b>'+'</b><br/> Status : <b>' +  t.Status  + '</b>'+'</b><br/> TAT : <b>' +  t.TAT__c+' <br/><br/><br/>Thanks & Regards <br/> <br/>Salesforce Administrator');
			
			//mail.setSaveAsActivity(true);
			System.debug('DEBUG :: SendReminderForTaskCompletion:: mail :: ->'+ mail);
			lstMail.add(mail);
        }
		
        System.debug('DEBUG : SendReminderForTaskCompletion:: lstMail :: ->'+ lstMail);
		
		if( lstMail.size() > 0 )
			Messaging.sendEmail(lstMail);
    }
}
If still there is any issue, please paste the values for all the system.debugs

Let me know if you need any other help.

Thanks,
Neetu
Debendra Ray 8Debendra Ray 8
Dear Neeetu,

I’ve used your amended code and now it’s throwing following error : 01:00:07.099 (1099311485)|VARIABLE_ASSIGNMENT|[11]|e|"common.apex.methods.MessagingStaticMethods$EmailExecutionException: SendEmail failed. First exception on row 28; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Failed to send email: []"|0x1223585a

Please could you suggest.

Thanks & Regards,

Debendra Ray
Neetu_BansalNeetu_Bansal
Hi,

As per the Governor Limits, you can only send 1000 single emails a day (or less, depending on license).

Using the API or Apex, you can send single emails to a maximum of 1,000 external email addresses per day based on Greenwich Mean Time (GMT). Single emails sent using the Salesforce application don't count toward this limit. There’s no limit on sending individual emails to contacts, leads, person accounts, and users in your organization directly from account, contact, lead, opportunity, case, campaign, or custom object pages.

For more details, please refer this:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

If you still have any issue, you can contact me on my personal ids specified in my profile.

Thanks,
Neetu
Debendra Ray 8Debendra Ray 8
Dear Neetu,

Please could you suggest, what changes are required in the above code to eliminate the SINGLE_EMAIL_LIMIT_EXCEEDED error. 

Thanks very much.

Regards,

Debendra Ray