Single Email Message Error -- REQUIRED_FIELD_MISSING, Missing target address

Salesforce Asked by Shantanu Mahajan on December 22, 2020

I have written a trigger in which I want to send an email to the Email address of Contact. But when i am testing the trigger it is giving me an error of

REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []:

Following is the code for trigger

trigger SendEmailMessageToFlatContact on Building_Wing__c (after update) {

    set<id> setValidBuildId = new set<id>();
    List<Flat__c> listFlat = new List<Flat__c>();
    List<String> ListOkEmails = new List<String>();
    List<String> ListPaymentEmails = new List<String>();
    map<String, Flat__c> mapEmailFlat = new map<String, Flat__c>();
    boolean Flag;

    for(Building_Wing__c objBuild:
        if(trigger.oldMap.get( != trigger.newMap.get(

    listFlat = [Select id, name, Primary_Contact__c,Building_Wing__c,Amount_Due_By_Stage__c,Building_Wing__r.Stage_Completed__c ,Primary_Email__c,Contact_Phone__c, Flat_Total_Cost__c, Amount_Received__c  from flat__c where Building_Wing__c In: setValidBuildId and Flat_Total_Cost__c >0];
    Decimal PaidPercentage = 0;
    String body;
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    EmailTemplate templateId = [Select id from EmailTemplate where DeveloperName= 'Installment_Due_Template'];    
    List<ID> SendTargetObject =  new List<ID>();
    for(flat__c objFlat: listFlat)
        PaidPercentage = (objFlat.Amount_Received__c / objFlat.Flat_Total_Cost__c)*100;
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();

        if(PaidPercentage < objFlat.Building_Wing__r.Stage_Completed__c && objFlat.Primary_Contact__c != null && objFlat.Primary_Email__c != null && objFlat.Contact_Phone__c != null)

            body = 'Dear Customer Please Pay Pending Amount::'+objFlat.Amount_Due_By_Stage__c;
            mapEmailFlat.put(objFlat.Primary_Email__c, objFlat);
            system.debug('IF Mail-------------->'+mail);

        else if(objFlat.Primary_Contact__c != null && objFlat.Primary_Email__c != null)
            body = 'Dear Customer New Stage has been Completed.';
           system.debug('else Mail-------------->'+mail);

    system.debug('ListPaymentEmails >>' +ListPaymentEmails + 'ListOkEmails >>' +ListOkEmails);
    // Email message to both group

      if(mails.size() > 0){


One Answer

Your error message: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc) refers to the Single Email Messaging Section of the Apex docs.

In this line: mail.setTargetObjectId(objFlat.Primary_Contact__c); you're trying to set the target object to an Id that's a field in objFlat which I presume contains a ContactId.

From the docs:

setTargetObjectId(targetObjectId) Required if using a template, optional otherwise. 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.

Since you're using a template, I'd check to see if all the necessary fields for the template are being set in your code for the merge fields. The error message is telling you that they're not.

Answered by crmprogdev on December 22, 2020

Add your own answers!

Related Questions

Apex Regex Extract Numeric Value from String

3  Asked on January 6, 2022 by yossi-cadaner


Ignore files when force:source:convert

4  Asked on January 6, 2022 by simonp


AuthToken using Open Id Connect

1  Asked on January 6, 2022 by bergin-panimayam


Using IsPickVal() function inside Email Templates

1  Asked on January 6, 2022 by anthony-williams


Create record by submitting string for reference field

1  Asked on January 4, 2022 by user85715


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP