+ Start a Discussion
sailersailer 

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.clear();
        usr_email.clear();
        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)

        {

            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.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    String [] toaddress=str;
    String[] ccAddresses=new String[]{'abc@gmail.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.setToAddresses(toaddress);
    email.setCcAddresses(ccAddresses);
    Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
   




   
    }

}
Ashish_SFDCAshish_SFDC
Hi Sailer, 


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


Regards,
Ashish
asish1989asish1989
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.
http://salesforceworld4u.blogspot.in/2013/12/sending-custom-object-information-in.html
Elie.RodrigueElie.Rodrigue
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.
asish1989asish1989
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
http://salesforceworld4u.blogspot.in/2013/12/sending-custom-object-information-in.html

Ashish_SFDCAshish_SFDC
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. 


Regards,
Ashish
Elie.RodrigueElie.Rodrigue
I made some sample code in my test org to validate that. Here's a sample package that does the job : 
https://login.salesforce.com/packaging/installPackage.apexp?p0=04tG0000000L4Ci

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.
Ashish_SFDCAshish_SFDC
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. 


Regards,
Ashish