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

Dynamic Email Template in Scheduler Apex

Hi Everyone,

I have Scheduler batch class and i need to trigger email if hard code the subject i can send out the mail,but i i need to call the email tempalet and take the row id of that object and send out mail .

Below is the code
global  class Remaindermail  implements Schedulable

   String FrequencyCount;
    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=[select id, Send_From_Email__c,FrequencyCheck__c,Status__c,Count__c from Property_Obj__c where FrequencyCheck__c=TRUE];
        Map_usr=new Map<string,string>();
        for(Property_Obj__c u:usr)







    public void sendmail(List<String> str)

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    String [] toaddress=str;
    String[] ccAddresses=new String[]{''};
      email.setSubject('Mass Email');
    email.setHtmlBody('Dear Sir /Madam,<br/><br/> Hello <br/><br/>Sincerely,<br><br/><br/>Date :'+string.valueof

    Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});


Hi Sailer, 

Is the code not working? whereexactly are you facing the issue. 

Here is the link. Please go through it. If you got some idea then don't forget to add comments and be a member of this blog.
In your sendemail method, you need to add email.setWhatId and pass your custom object id as a parameter. Then your email template can you {!CustomObject.CustomField__c} as tokens.
However you'll need to send the email to a contact and need to call email.setTargetObjectId(contactId) otherwise it wont work.

An ugly work around would be to create a temporary contact with the email address you want to send it to, send the email then delete the contact.
Elie.Rodrigue is right. But there will be still one problem.Supoose in your template you are using merge field of Property_Obj__c then that field will never be populated with value dynamically.  
For example your are using like this in your template 
Dear {!Property_Obj__c.Name}  
then this value will never be populated because you are passing contactId in setTargetObjectId. Even you can't pass other object id in that method except lead,contact and user. That's why I would suggest dont use email tempolate instead of that create a email template in your code.
Here is the link

Hi All, 

Instead will this be possible to
Create a text field(Object_Name2__c),
hide it from page layout,
write a formula to copy the Subject into the new field. 
Call that new field in the code - {!Object_Name__c} 

Where a new custom field will work instead of the standard field. 

I made some sample code in my test org to validate that. Here's a sample package that does the job :

Add the custom object related list to your page layout. Create a custom object for the contact and enter a description. Make sure the datetime field is left null.

The schedule the class to run and it should send the email with the value from the custom object.

SetWhatId and SetTargetObject is the solution you need. Maybe Ashish's issue is that activity is not enabled on the custom object.
Hi Elie, 

I was trying to throw my logic with a duplicate field to see if that works not an issue as such. 

I have installed the package, great to see that you have also included the test class  - it looks good and solves the purpose.