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
vivek ravivivek ravi 

can any one say what error in this code

public PageReference send()
    {
        List<Contact> contacts=[Select Id,Email From Contact Where AccountId='001i000000g7erl'];
        List<Id> Conids=new List<string>();
        for(Contact mail: contacts)
        {
            Conids.add(mail.Email);
        }
      
        Messaging.MassEmailMessage emails=new Messaging.MassEmailMessage();
        emails.setTargetObjectIds(Conids);
        emails.setTemplateId('00Xi0000000J9hx');
        emails.setsubject('note');
        emails.setplainTextBody('body');
      
        Messaging.SendEmail(New Messaging.MassEmailMessage[] {emails});

return null;
}

when im clicking the send button
the error is ::System.StringException: Invalid id: vivekravi@gmail.com
Error is in expression '{!send}' in component <apex:commandButton> in page brand

this the error can any one solve this error for me
Best Answer chosen by vivek ravi
Chidambar ReddyChidambar Reddy
Hi Vivek, Try the following.

public PageReference send()
    {
        List<Contact> contacts=[Select Id,Email From Contact Where AccountId='001i000000g7erl'];
        List<Id> Conids=new List<string>();
        for(Contact mail: contacts)
        {
            Conids.add(mail.Id);
        }
     
        Messaging.MassEmailMessage emails=new Messaging.MassEmailMessage();
        emails.setTargetObjectIds(Conids);
        emails.setTemplateId('00Xi0000000J9hx');
        emails.setsubject('note');
        emails.setplainTextBody('body');
     
        Messaging.SendEmail(New Messaging.MassEmailMessage[] {emails});

return null;
}

Reference : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm

Thank you,
Choose it as best answer, If it resolved your issue.

All Answers

Chidambar ReddyChidambar Reddy
Hi Vivek, Try the following.

public PageReference send()
    {
        List<Contact> contacts=[Select Id,Email From Contact Where AccountId='001i000000g7erl'];
        List<Id> Conids=new List<string>();
        for(Contact mail: contacts)
        {
            Conids.add(mail.Id);
        }
     
        Messaging.MassEmailMessage emails=new Messaging.MassEmailMessage();
        emails.setTargetObjectIds(Conids);
        emails.setTemplateId('00Xi0000000J9hx');
        emails.setsubject('note');
        emails.setplainTextBody('body');
     
        Messaging.SendEmail(New Messaging.MassEmailMessage[] {emails});

return null;
}

Reference : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm

Thank you,
Choose it as best answer, If it resolved your issue.
This was selected as the best answer
Pravi_1133Pravi_1133
Vivek,

Adding Emails to the list is causing exception I feel. Try just addding Id to the list Conids and also change the intialization part of list Conids.
List<Id> Conids=new List<id>();
        for(Contact mail: contacts)
        {
            Conids.add(mail.id);
        }
For mass email, no need to mention Subject, tr after removing subject field also.
Let me know if this approach helps you.


---Praveen.