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
Varsha D 9Varsha D 9 

when i m trying to send mail along with template getting error as Method does not exist or incorrect signature: void setTargetObjectId(List<Id>) from the type Messaging.SingleEmailMessage

User-added imageUser-added imageglobal class Email2 {
    @HttpGet
    global static void sendMail(){
list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
 
list <String> toAddresses = new list<String>();
 toAddresses.add('varsha.dustakar7@gmail.com');
 mails.setToAddresses(toAddresses);
 
List<Contact> con = [SELECT Id, firstname FROM Contact];
 List<Id> lstids= new List<Id>();
 for(Contact c:con) {
  lstids.add(c.id);
 }
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
mails.setTargetObjectId(lstids); 
mails.setTemplateId(et.id);
mail.add(mails); 
Messaging.sendEmail(new Messaging.singleEmailMessage[] { mails });
    }
}
Best Answer chosen by Varsha D 9
Maharajan CMaharajan C
Hi Varsha,

When we use the contact in mail.setTargetObjectId it's mandatory the particular contact email address needs to populated. 

I understood what you are trying. But the contact you are trying to find using postman  must be filled with email address otherwise you will get this error. To avoid this you have to remove the contact which is not having the email by using email null check in apex like below.
List<Contact> con = [SELECT Id,Email,firstname FROM Contact where Email != null ];

And one more think that email address will be added in email to list automatically i think... Please verify this point...

if you are not comfort with above things then you have to remove the template reference and setTargetObjectId from your code and you have to build the HTML body dynamically in apex runtime instead of email template and this html body needs to be attached in email . 

Thanks,
Maharajan.C

All Answers

Kumar995Kumar995
try to pass UserInfo.getUserId() in setTargetObjectId
Maharajan CMaharajan C
Hi Varsha,

setTargetObjectId will accept only one id at a time but you are list<Id> in above so you are getting this error.

Your code should be like below:
 
@HttpGet
global static void sendMail(){
	list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
	EmailTemplate et = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate WHERE developerName = 'gmail_salesforce'];
	List<Contact> con = [SELECT Id,Email,firstname FROM Contact];
	for(Contact c:con){
		Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
		list <String> toAddresses = new list<String>();
		toAddresses.add('varsha.dustakar7@gmail.com');
		mails.setToAddresses(toAddresses);
		mails.setTargetObjectId(c.Id); 
		mails.setTemplateId(et.id);
		mail.add(mails); 
	}
	Messaging.sendEmail(new Messaging.singleEmailMessage[] { mails });
}

Thanks,
Maharajan.C
Varsha D 9Varsha D 9
User-added image

Hi,Maharanjan
 when i tired to execute i m getting this below error as  variable does not exist
 can you help me out with this 

Thankyou

 
Maharajan CMaharajan C
Hi Varsha,

Please try the below code:
 
@HttpGet
global static void sendMail(){
    list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
    EmailTemplate et = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate WHERE developerName = 'gmail_salesforce'];
    List<Contact> con = [SELECT Id,Email,firstname FROM Contact];
    for(Contact c:con){
        Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
        list <String> toAddresses = new list<String>();
        toAddresses.add('varsha.dustakar7@gmail.com');
        mails.setToAddresses(toAddresses);
        mails.setTargetObjectId(c.Id); 
        mails.setTemplateId(et.id);
        mail.add(mails); 
    }
    Messaging.sendEmail(new Messaging.singleEmailMessage[] { mail });   // Remove s from mail
}

Thanks,
Maharajan.C
Varsha D 9Varsha D 9
User-added image

Hi Maharajan,

try the above code which you sent getting Initial expression is of incorrect type, expected: Messaging.SingleEmailMessage but was: List<Messaging.SingleEmailMessage> as issue
Maharajan CMaharajan C
Sorry my mistake:

Messaging.sendEmail(new Messaging.singleEmailMessage[] { mail });  ==>  Messaging.sendEmail(mail);

Use the below code:
 
@HttpGet
global static void sendMail(){
    list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
    EmailTemplate et = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate WHERE developerName = 'gmail_salesforce'];
    List<Contact> con = [SELECT Id,Email,firstname FROM Contact];
    for(Contact c:con){
        Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
        list <String> toAddresses = new list<String>();
        toAddresses.add('varsha.dustakar7@gmail.com');
        mails.setToAddresses(toAddresses);
        mails.setTargetObjectId(c.Id); 
        mails.setTemplateId(et.id);
        mail.add(mails); 
    }
    Messaging.sendEmail(mail);  
}

Thanks,
Maharajan.C
Varsha D 9Varsha D 9
User-added image

Hi Maharajan,
Getting Run time exception as:
​​​​​​​System.EmailException: SendEmail failed. First exception on row 1; first error: INVALID_EMAIL_ADDRESS, The target object's email address "null" is not valid: [targetObjectId, 0035g000006KndyAAC]
Maharajan CMaharajan C
Update the contact query as below:
 
List<Contact> con = [SELECT Id,Email,firstname FROM Contact where Email != null Limit 10];

Thanks,
Maharajan.C
Varsha D 9Varsha D 9
User-added image

Hi MahaRajan,
Firstly, Thanks for Staying Patient helping me out  ,
Now i m getting the issue as :The target Objects Email Address "Null" is not valid 

My intension in this code is to whenever i specify any contact record id in rest web service like Postman 
it has to give me details of that contact record specified in my template .
Maharajan CMaharajan C
Hi Varsha,

When we use the contact in mail.setTargetObjectId it's mandatory the particular contact email address needs to populated. 

I understood what you are trying. But the contact you are trying to find using postman  must be filled with email address otherwise you will get this error. To avoid this you have to remove the contact which is not having the email by using email null check in apex like below.
List<Contact> con = [SELECT Id,Email,firstname FROM Contact where Email != null ];

And one more think that email address will be added in email to list automatically i think... Please verify this point...

if you are not comfort with above things then you have to remove the template reference and setTargetObjectId from your code and you have to build the HTML body dynamically in apex runtime instead of email template and this html body needs to be attached in email . 

Thanks,
Maharajan.C
This was selected as the best answer