You need to sign in to do that
Don't have an account?
getting SOQL statement to return a string to be used in an email template?
I have made an Apex Scheduler Class for Birthdays. The class works and successfully sends an email to our director when a birthday is 2 days away.
However, the email template that is sent when there is a birthday two days away needs to contain the contact's first, last name and birthdate. (The email template looks like this: This is a scheduler-generated email to notify you that the birthday of First Name: {!Contact.FirstName} , Last Name: {!Contact.LastName} is on {!Contact.Next_Birthday__c}.
In my scheduler, I use a SOQl statement to query the database for the contact's id and first name.
After I call the query, how do I convert the information I just queried to a string/method to be used in the email template?
My guess is return the results of the query as a string? Any Advice is appreciated!
Here is my code:
global class BirthdayName implements Schedulable{
global void execute (SchedulableContext ctx)
{
sendBirthdayEmail();
}
public void sendBirthdayEmail()
{
for(Contact con : [SELECT Contact.name, Id FROM Contact WHERE Next_Birthday__c = : system.Today().addDays(2)])
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XJ0000000M31w');
mail.setTargetObjectId('005J0000000JWYx');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}
}
I'm not trying to send the email to the contact whose birthday is in 2 days. I'm sending a reminder email to the director that an employee that (!contact.firstname), (!contact.lastname) has a birthdate on: (!contact.Next_Birthday__c).
In the query, I get the contact id and first/last name of the contact.
How do I return the information I just queried (contact id, first/last name of contact) as a string? (So I can put that string in the email template).
Thanks....but how does the email template understand what information to pull from.
Would the new template be: (!con.Name) with Salesforce ID of (!con.Id) has a Birthday in 2 days.
Would the new template understand these values (con.name) and (con.id)?
I think you might have been asking the wrong question. You shouldn't need to return items in the query to add to an email template. I think all you need to do is specify the Contacts Id like I showed in the first response. In your actual email template, you should be able to select the specific fields you want in it.
I could be wrong....but I do think I need to return items in the query to add to an email template.
I ran a test and the template did not show the con.id , con.name or {!con.id} {!con.name} or {!Contact.Id} or {!Contact.Name}
I think the template doesn't know where to pull the contact information from. How do I link my Apex Scheduler to the email template as in how will my email template understand to pull the contact information from the contact with a birthday 2 days away.
Does an email template understand the strings I declared in apex? Should it understand them?
Did you put this line in? Your query doesn't matter at all as far as i know. You just need to provide the Id.