You need to sign in to do that
Don't have an account?
Email Template based on specific record. E.G. Transaction Receipt
Hi there,
I was wondering if it were possible to create a visualforce email template based on a specific record.
I will explain my situation.
I have account and transactions__c is a custom object which is a child or Account. I was wondering if it is possible to create an email template which basically shows all the fields related to the transaction. Then I could either write a trigger which will send said email template whenever a transaction record is created or use my current email trigger to reference a specific record and then send out an email with the transaction information.
Thank you for your time
I was wondering if it were possible to create a visualforce email template based on a specific record.
I will explain my situation.
I have account and transactions__c is a custom object which is a child or Account. I was wondering if it is possible to create an email template which basically shows all the fields related to the transaction. Then I could either write a trigger which will send said email template whenever a transaction record is created or use my current email trigger to reference a specific record and then send out an email with the transaction information.
Thank you for your time
trigger SendNewTransactionEmail on Transaction__c (after insert) {
//Get the correct email template here
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Your Template Name'];
Set<Id> accountIds = new Set<Id>();
for (Transaction__c transaction : trigger.new) {
accountIds.add(transaction.Account__c);
}
List<Account> accounts = [SELECT Id, (SELECT Id, Email FROM Contacts) FROM Account WHERE Id IN :accountIds];
Map<Id, List<Contact>> accountMap = new Map<Id, List<Contact>>();
for (Account acct : accounts) {
List<Contact> contacts = new List<Contact>();
for (Contact c : acct.Contacts) {
contacts.add(c);
}
accountMap.put(acct.Id, contacts);
}
//It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
for (Transaction__c transaction : trigger.new) {
for (Contact c : accountMap.get(transaction.Account__c) {
if (c.Email != null) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
m.setTargetObjectId(c.Id);
m.setWhatId(transaction.Id);
messages.add(m);
}
}
}
Messaging.sendEmail(messages);
}
All Answers
If you're sending from an Apex trigger you can use the setTargetObjectId() and setWhatId() methods on SingleEmailMessage to specify the two records that you are using.
trigger SendNewTransactionEmail on Transaction__c (after insert) {
//Get the correct email template here
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Your Template Name'];
//It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
for (Transaction__c transaction : trigger.new) {
//You could add some logic here to only send the email only for certain transactions, if necessary
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
//You'll need to somehow figure out the correct contact ID here
m.setTargetObjectId(contactId);
m.setWhatId(transaction.Id);
messages.add(m);
}
Messaging.sendEmail(messages);
}
trigger SendNewTransactionEmail on Transaction__c (after insert) {
//Get the correct email template here
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Your Template Name'];
Set<Id> accountIds = new Set<Id>();
for (Transaction__c transaction : trigger.new) {
accountIds.add(transaction.Account__c);
}
List<Account> accounts = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds];
Map<Id, List<Id>> accountMap = new Map<Id, List<Id>>();
for (Account acct : accounts) {
List<Id> contactIds = new List<Id>();
for (Contact c : acct.Contacts) {
contactIds.add(c.Id);
}
accountMap.put(acct.Id, contactIds);
}
//It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
for (Transaction__c transaction : trigger.new) {
for (Contact c : accountMap.get(transaction.Account__c) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
m.setTargetObjectId(c.Id);
m.setWhatId(transaction.Id);
messages.add(m);
}
}
Messaging.sendEmail(messages);
}
for (Transaction__c transaction : trigger.new) {
for (Id contactId : accountMap.get(transaction.Account__c) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
m.setTargetObjectId(contactId);
m.setWhatId(transaction.Id);
messages.add(m);
}
}
thank you so much, it is working. I just had a quick question. Before I mark your solution as correct.
the trigger seems to fail, should one of the contacts not have an email address. Is there anyway around this?
trigger SendNewTransactionEmail on Transaction__c (after insert) {
//Get the correct email template here
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'Your Template Name'];
Set<Id> accountIds = new Set<Id>();
for (Transaction__c transaction : trigger.new) {
accountIds.add(transaction.Account__c);
}
List<Account> accounts = [SELECT Id, (SELECT Id, Email FROM Contacts) FROM Account WHERE Id IN :accountIds];
Map<Id, List<Contact>> accountMap = new Map<Id, List<Contact>>();
for (Account acct : accounts) {
List<Contact> contacts = new List<Contact>();
for (Contact c : acct.Contacts) {
contacts.add(c);
}
accountMap.put(acct.Id, contacts);
}
//It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
for (Transaction__c transaction : trigger.new) {
for (Contact c : accountMap.get(transaction.Account__c) {
if (c.Email != null) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTemplateId(template.Id);
m.setTargetObjectId(c.Id);
m.setWhatId(transaction.Id);
messages.add(m);
}
}
}
Messaging.sendEmail(messages);
}
Thank you so much for your time. You have been an amazing help! I could not have done it without you. I will probably swap my other email code for this!
For all future thread viewers, should cheynes code error slightly the email just has to be querries in the SOQL. End code as follows: