You need to sign in to do that
Don't have an account?

Can't use email template when sending email via AIR
When sending an email using the Salesforce API via AIR I can successfully send a HTML email like this:
var message:SingleEmailMessage = new SingleEmailMessage();
message.senderDisplayName = "SenderName";
message.replyTo = "sender@name.com";
message.saveAsActivity = true;
message.targetObjectId = recipientID;
message.subject = _emailSubject;
message.htmlBody = _htmlBody;
var messages:Array = [message];
airConnection.sendEmail(messages, new AsyncResponder(
But if I try to set a template ID the sending fails with no obvious error
var message:SingleEmailMessage = new SingleEmailMessage();
message.senderDisplayName = "SenderName";
message.replyTo = "sender@name.com";
message.targetObjectId = recipientID;
message.templateId = "Sample_HTML_Mail";
var messages:Array = [message];
airConnection.sendEmail(messages, new AsyncResponder(
The template does exist and the ID I'm specifying is the Template Unique Name specified in Salesforce.
Would anyone have any ideas why this is happening and how I might fix it?
Thanks,
Mark
var message:SingleEmailMessage = new SingleEmailMessage();
message.senderDisplayName = "SenderName";
message.replyTo = "sender@name.com";
message.saveAsActivity = true;
message.targetObjectId = recipientID;
message.subject = _emailSubject;
message.htmlBody = _htmlBody;
var messages:Array = [message];
airConnection.sendEmail(messages, new AsyncResponder(
But if I try to set a template ID the sending fails with no obvious error
var message:SingleEmailMessage = new SingleEmailMessage();
message.senderDisplayName = "SenderName";
message.replyTo = "sender@name.com";
message.targetObjectId = recipientID;
message.templateId = "Sample_HTML_Mail";
var messages:Array = [message];
airConnection.sendEmail(messages, new AsyncResponder(
The template does exist and the ID I'm specifying is the Template Unique Name specified in Salesforce.
Would anyone have any ideas why this is happening and how I might fix it?
Thanks,
Mark
All Answers
The template is definitely marked as Available for Use.
Regarding the folder sharing, I'm assuming that if I'm logged into the salesforce.com dashboard with the same username as I'm using with the airconnection, and I can see the folder in which the template resides, it is being shared with this user.
Would you have any other anything else to suggest?
Can you try the same code through Salesforce developer console once?
The template I'm using simply has 'Test Template' in the body. No references to any fields.
Unfortunately I don't know how to try sending a template email through the developer console and can't seem to find any references/tutorials :(
public void doSendEmail() {
try {
SingleEmailMessage message = new SingleEmailMessage();
message.setReplyTo('person1@salesforce.com');
message.setSaveAsActivity(false);
message.setSubject('This is how you use the sendEmail method');
message.setPlainTextBody('This is the humongous body');
message.setToAddresses(new String[] {'name@domain.com'});
SingleEmailMessage[] messages = { message };
SendEmailResult[] results = connection.sendEmail(messages);
if (results[0].isSuccess()) {
System.out.println('The email was sent successfully');
} else {
System.out.println('The email failed to send'+ results[0].getErrors()[0].getMessage());
}
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}
doSendEmail();
String[] toAddresses = new String[] {'bhavi@simplyforce.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Test');
mail.setPlainTextBody('Test');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Now I'm trying to change this to a template email.
I get ... Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTemplateId(LIST<String>)
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'mark@crooks.co.uk'};
mail.setToAddresses(toAddresses);
String[] templateID = new String[] {'TestTemplate'};
mail.setTemplateId(templateID);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'mark@crooks.co.uk'};
mail.setToAddresses(toAddresses);
EmailTemplate et=[Select id from EmailTemplate where name=:'TestTemplate'];
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
I'm getting: System.QueryException: List has no rows for assignment to SObject
1. You cannot use setToAddresses with a template. You have to use a targetObjectId
2. When specifying which template to use you specify the 'Email Template Name', not the 'Template Unique Name', which is a little confusing as the SalesForce UI mentions that the Template Unique Name is used by the API.
Now I just need to figure out how to implement this inside AIR ;).
Once again Bhawani, thanks for your help and patience.
contact con=[Select id from contact limit 1];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id);
mail.setSenderDisplayName('Mark');
EmailTemplate et=[Select id from EmailTemplate where name='Test'];
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Just discovered that as the Apex query runs in the developer console I can grab the Template ID string from there and set this within AIR.
ie. message.templateId = "KDD84995JFFKJSSKFK";
:)