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
NY70NY70 

REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc)

Can anyone help me undertand why this happens in one of my classes and not in the another, even when the code is basically the same?

 

I have two classes which are basically looking for two different types of procedures on my custom object's records.

I get a list of records for which I have to send an email to.  On the help files for SingleEmailMessage, this methods state that:

 

setTargetObjectIdIDVoidOptional. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

Do not specify the IDs of records that have the Email Opt Out option selected.

All email must have a recipient value of at least one of the following:
  • toAddresses
  • ccAddresses
  • bccAddresses
  • targetObjectId
  • targetObjectIds
setToAddressesString[]VoidOptional. A list of email address to which you are sending the email. The maximum number of email addresses allowed is 100. This argument is allowed only when a template is not used.
All email must have a recipient value of at least one of the following:
  • toAddresses
  • ccAddresses
  • bccAddresses
  • targetObjectId
  • targetObjectIds

 

According to this help, I can either use setToAddresses or setTargetObjectId to setup the email address or OwnerId.  This works fine in one of my classes without any errors, but not in the other. Here is the code for both classes. 

 

class 1:

// Send an email to each account owner
list<Messaging.SingleEmailMessage> el = new list<Messaging.SingleEmailMessage>();
for(Actuals_Volume__c rec : recsList){
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(rec.Account__r.OwnerId);
   mail.setSaveAsActivity(false);
   mail.setSenderDisplayName('Salesforce Support');
   mail.setPlainTextBody('Your account: ' + rec.Account__r.Name +' has not ordered MGA in the past 30 days.');
   mail.setHtmlBody('Your account:<b> ' + rec.Account__r.Name +' </b>has not ordered MGA in the past 30 days.<p>'+
   'To view the account <a href=https://na11.salesforce.com/' + rec.Account__r.Id + '>click here</a>');
   el.add(mail);
}
system.debug('==========>>> el.size = ' + el.size());
		
if(!el.isEmpty()){
   Messaging.sendEmail(el);  <---- No error here at all
}

 Class 2:

// Send an email to each account owner
list<Messaging.SingleEmailMessage> el = new list<Messaging.SingleEmailMessage>();
for(Actuals_Volume__c rec : recsList){
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(rec.Account__r.OwnerId);
   mail.setSaveAsActivity(false);
   mail.setSenderDisplayName('Salesforce Support');
   mail.setPlainTextBody('Your account: ' + rec.Account__r.Name +' has not ordered Hematuria in the past 7 days.');
   mail.setHtmlBody('Your account:<b> ' + rec.Account__r.Name +' </b>has not ordered Hematuria in the past 7 days.<p>'+
   'To view the account <a href=https://na11.salesforce.com/' + rec.Account__r.Id + '>click here</a>');
   el.add(mail);
}
system.debug('==========>>> el.size = ' + el.size());
		
if(!el.isEmpty()){
   Messaging.sendEmail(el);  <-- get error here
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Thats gr8 that it helped you,

 

I would request you to mark it as accepted solution so that others can also get benifited from it.

All Answers

Shashikant SharmaShashikant Sharma

I would just want to know in both cases while you test is this rec.Account__r.OwnerId has same means are you testing for same account in booth the cases. Please verify if you are using twodiffent account with different owner then there must be some data issue with the one which giveing error mostl probably in email field of user.

NY70NY70

Thanks to your statement about a different owner having some data issues, it got me thinking that since I had deployed this to production about 2 months ago and I had had no issues since, there had to be something new going on this week.

 

I overlooked the obvious, some of my custom object records did not have an account linked to them which was causing the problem when assigning the Account.OwnerId to the email.  I fixed the records and the problem went away.

 

To avoid this issue in the future, I made some changes to the apex code to avoid records that have no accounts linked to them.  I also created a report in SFDC that will tell me if there are such records in the system.  And to really make sure delinquent records do not make it into the system, I made some changes to the autoload process.

 

Thanks for your help !!!

Shashikant SharmaShashikant Sharma

Thats gr8 that it helped you,

 

I would request you to mark it as accepted solution so that others can also get benifited from it.

This was selected as the best answer