You need to sign in to do that
Don't have an account?
Varsha 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
global 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 });
}
}
@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 });
}
}
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
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:
Thanks,
Maharajan.C
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
Please try the below code:
Thanks,
Maharajan.C
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
Messaging.sendEmail(new Messaging.singleEmailMessage[] { mail }); ==> Messaging.sendEmail(mail);
Use the below code:
Thanks,
Maharajan.C
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]
Thanks,
Maharajan.C
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 .
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