You need to sign in to do that
Don't have an account?
sailer
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});
}
}
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});
}
}
Is the code not working? whereexactly are you facing the issue.
Regards,
Ashish
http://salesforceworld4u.blogspot.in/2013/12/sending-custom-object-information-in.html
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.
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
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
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.
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