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
sailersailer 

mass mail for custom object using the template

Hi

I wrote the code for sending out the mail for cutom object "I am getting the following error"   / SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Only contacts, leads and users allowed for targetObjectIds.: []

below is the code

/*
Created for Followp and to send the mail


*/

global  class Remaindermail  implements Schedulable
{

    public List<Property_Obj__c> usr{get;set;} //
    public List<String> usr_email;
// List<Property_Obj__c> updateproperty= new List<Property_Obj__c>();
  Property_Obj__c updateproperty= new Property_Obj__c();
   public Map<string,string> Map_usr{get;set;}
 
   
    global void execute(SchedulableContext ctx)
    {
       
       
      List<Property_Obj__c> lstcon=[select id, Listing_Sales_Associate_Email__c,Mail_Response__c from Property_Obj__c where Mail_Response__c=true];
  List<Id> lstids= new List<Id>();
  for(Property_Obj__c c:lstcon){
  lstids.add(c.id);
  //lstids.add(c.Listing_Sales_Associate_Email__c);
  }
  EmailTemplate et=[Select id from EmailTemplate where name=:'MailTemplate'];
  Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
  //mail.setWhatId(c.ID);
  mail.setTargetObjectIds(lstIds);
  //mail.setTargetObjectIds(lstIds)
  mail.setSenderDisplayName('TestMailFromSFDC');
  mail.setTemplateId(et.id);
  Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}

}

Pl help me out
harsha__charsha__c
The error given is cirrect. You can set only Ids of either one of Contact, Lead and User objects.

If you would like to send mails to the email Ids, which are stored in the custom object records, you can query them all and grab all the email Ids into a list and pass it to the email.setToAddresses.

By the way You need to set the targetOObjectId madatorily, while you use email template. So, just pass any UserId there.

Also, you can achieve this by singleEmailMessage itself(sending email to multiple at once)

Hope this helps..! Feel free to poke back in case of any queries

Regards,
- Harsha
sailersailer
/*
Created for Followp and to send the mail


*/

global  class RemainderMailFrequency implements Schedulable
{

    public List<Property_Obj__c> usr{get;set;} //
    public List<String> usr_email{get;set;}
// List<Property_Obj__c> updateproperty= new List<Property_Obj__c>();
  Property_Obj__c updateproperty= new Property_Obj__c();
   public Map<string,string> Map_usr{get;set;}
 
   
    global void execute(SchedulableContext ctx)
    {
        usr=new List<Property_Obj__c>();
        usr_email=new List<String>();
        usr.clear();
        usr_email.clear();
        usr=[select id, Send_From_Email__c,Status__c from Property_Obj__c where Status__c='Emailed to Customer' ];
        Map_usr=new Map<string,string>();
        for(Property_Obj__c u:usr)

        {

            system.debug('_____________id______'+u.id+'___________Email______________'+u.Send_From_Email__c+'_________DATE______________________________________'+u.Status__c);
            Map_usr.put(u.id,u.Send_From_Email__c);
            if(u.Send_From_Email__c!=null)
            usr_email.add(u.Send_From_Email__c);
                   }

       if(usr_email.size()>0)
{

           sendmail(usr_email);
          
}

    }
    public void sendmail(List<String> str)

    {
   
     Messaging.Massemailmessage email = new Messaging.Massemailmessage();
     String [] toaddress=str;
     String[] ccAddresses=new String[]{'test@abc.com'};
     email.setSubject('Mass Email');
   //email.setHtmlBody('Dear Sir /Madam,<br/><br/> Hello <br/><br/>Sincerely,<br><br/><br/>Date :'+string.valueof(system.today()));
    email.setTemplateId('00XS0000000MOrJ');
      email.setTargetObjectIds('');//Error at this line Save error: Method does not exist or incorrect signature: Messaging.MassEmailMessage].setTargetObjectIds(String)
[Messaging.MassEmailMessage].setTargetObjectIds(String)
      email.setToAddresses(toaddress);
      email.setCcAddresses(ccAddresses);
      Messaging.sendEmail(new Messaging.MassEmailMessage[] { email });
        
    }

}
harsha__charsha__c
Hello Sailer,

email.setTargetObjectIds(''); can not be left blank. 

Try this:

email.setTargetObjectIds(new List<Id>{UserInfo.getUserId()});


Regards,
- Harsha


sailersailer
Hi Harsha,

Thanks a lot i have hard corded the user ids ,since the mail goes from admin user,
I created the HTML template ,like Subject: {!Property_Obj__c.Listing_Sales_Associate_Email__c} ::EmailIds
these are the values i need to send to the customer ,But when i send the mail from the above Scheduler class the mail is getting triggred  ,but i am not able to get the values in the mail ,

The values are not picking at run time from the template .

Can you please help me out.

Regards,
Sailer
harsha__charsha__c
mail.setWhatId(c.ID);

Did you try passing the whatId by as above to the email template?

If you face this issue, though you pass whatId parameter, then pls try a visualforce template instead HTML template. I have tried with Visualforce template and seen success with it.

Reference for VF templates : http://wiki.developerforce.com/page/VisualForceEmailTemplates_sample

Regards,
- Harsha
sailersailer
Thanks Harsha,

I have created teh VF template

I wrote the code by VF template and components ,in the VF template i am getting all the records that have checked,but i need one records and send the mail for that record.
and i am scheduling the code that run every day to pick that template and send out the mail

Here is the code.

######Compnent Class#####
<apex:component controller="acctTemplt" access="global">
    <apex:attribute name="AcctId" type="Id" description="Id of the account" />
    <table border = "2" cellspacing = "5">
        <tr>
            <td>Name</td>
            <td>Status</td>             
        </tr>
        <apex:repeat value="{!opptys}" var="o">
        <tr>
            <td>{!o.Name}</td>
            <td>{!o.Mail_Response__c  }</td>           
        </tr>
       </apex:repeat>
          
    </table>
</apex:component>
########Apex Class#######
public  class acctTemplt
{
   public List<String> usr_email{get;set;}
   public List<Property_Obj__c> usr{get;set;} //
   public Map<string,string> Map_usr{get;set;}
    public List<Property_Obj__c> getopptys()
    {
        usr=new List<Property_Obj__c>();
  usr_email=new List<String>();
        List<Property_Obj__c> oppty;
         Map_usr=new Map<string,string>();
        oppty = [SELECT Id,Name, Mail_Response__c,Listing_Sales_Associate_Email__c,Status__c  FROM Property_Obj__c WHERE Mail_Response__c =true ];
         return oppty;
    }
}
#####Scheduler Class#######
/*
Created for Followp and to send the mail


*/

global  class RemainderMailFrequency implements Schedulable
{

    public List<Property_Obj__c> usr{get;set;} //
    public List<String> usr_email{get;set;}
    Property_Obj__c updateproperty= new Property_Obj__c();
    public Map<string,string> Map_usr{get;set;}
 
   global void execute(SchedulableContext ctx)
    {
       usr=new List<Property_Obj__c>();
       usr_email=new List<String>();
       usr.clear();
       usr_email.clear();
       usr=[select id, Status__c,Name,Mail_Response__c,Listing_Sales_Associate_Email__c from Property_Obj__c where Mail_Response__c=true ];
       Map_usr=new Map<string,string>();
       for(Property_Obj__c u:usr)
       {
        system.debug('__id______'+u.id+'___________Email______'+u.Listing_Sales_Associate_Email__c+'_________DATE_____'+u.Status__c);
        Map_usr.put(u.id,u.Listing_Sales_Associate_Email__c);
        if(u.Listing_Sales_Associate_Email__c!=null)
        usr_email.add(u.Listing_Sales_Associate_Email__c);
       }
       if(usr_email.size()>0)
    {
          sendmail(usr_email);
       }
   }
 
     public void sendmail(List<String> str)
     {
     
       EmailTemplate et = [SELECT id,HtmlValue,Body,Subject ,Name FROM EmailTemplate WHERE Name = 'RemainderMailVF'];
       system.debug('Template values for sending the mail__________________ '+et.Subject);
       Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
       String [] toaddress=str;
       String[] ccAddresses=new String[]{'test@gmail.com'};
       email.setTemplateId(et.Id);
       email.setTargetObjectId('005800000059yaE');
     email.setSaveAsActivity(false);
    email.setToAddresses(toaddress);
    email.setCcAddresses(ccAddresses);
    Messaging.sendEmail(new Messaging.Singleemailmessage[] { email });
        
     }

}

and my o/p Look like below ,but there will be one Name for one email id associated i need to this alone

Name  Status
13  true
12  true
15  true
14  true

Thanks for the Help.

Regards
Sailer
harsha__charsha__c
Name  Status
13  true
12  true
15  true
14  true

I believe that you are using the component directly in the vf template and there by your email is showing all the data that component renders/shows.

Make your component to show only one record. then obviously, you see the same in your email as well.

Regards,
- Harsha
sailersailer
HI Harsha,

Whic Tag i need to use to show olny one record in the component.
I am struck badly here can you pl help out
harsha__charsha__c
Hope you might be using <apex:repeat value="{!sampleList}">XXXXXXXXXXXXX</apex:repeat>

#1. Make the sampleList contain only one record, which you want to display.
#2. Add rows="1" attribute to the apex:repeat component, which shows only the first record from the list always.
Like, <apex:repeat value="{!sampleList}" rows="1">XXXXXXXXXXXXX</apex:repeat>

Make use either one of the above suggestions. 

Regards,
- Harsha