1

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: trigger.new)
    {
        if(trigger.oldMap.get(objBuild.id).Stage_Completed__c != trigger.newMap.get(objBuild.id).Stage_Completed__c)
        {
            setValidBuildId.add(objbuild.id);
        }
    }

    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)
        {

            ListPaymentEmails.add(objFlat.Primary_Email__c);
            body = 'Dear Customer Please Pay Pending Amount::'+objFlat.Amount_Due_By_Stage__c;
            SMSUtil.sendSms(objFlat.Contact_Phone__c,body);
            mapEmailFlat.put(objFlat.Primary_Email__c, objFlat);
            mail.setToAddresses(ListPaymentEmails);
            mail.setTargetObjectId(objFlat.Primary_Contact__c);
            mail.setTemplateID(templateId.Id); 
            mail.setSaveAsActivity(true);
            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.';
            SMSUtil.sendSms(objFlat.Contact_Phone__c,body);
            ListOkEmails.add(objFlat.Primary_Email__c);
            mail.setToAddresses(ListPaymentEmails); 
            mail.setTargetObjectId(objFlat.Primary_Contact__c);
            mail.setTemplateID(templateId.Id); 
            mail.setSaveAsActivity(true);
           system.debug('else Mail-------------->'+mail);
        }

        mails.add(mail);   
    }
    system.debug('ListPaymentEmails >>' +ListPaymentEmails + 'ListOkEmails >>' +ListOkEmails);
    // Email message to both group
    system.debug('Mails------------->'+mails);

      if(mails.size() > 0){
          Messaging.sendEmail(mails);
      }

}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.