function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mark CrooksyMark Crooksy 

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
Best Answer chosen by Mark Crooksy
Bhawani SharmaBhawani Sharma
Please follow this: http://salesforceondemand.blog.com/2012/01/27/to-send-email-from-apex-using-email-template/

All Answers

Bhawani SharmaBhawani Sharma
Can you please make sure that template has "Available for Use" checkbox to true? Also the folder in which email template is, shared with the airconnection user.
Mark CrooksyMark Crooksy
Thanks for your reply Bhawani.

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?
Bhawani SharmaBhawani Sharma
Is there any possibility your are trying to use record or some fields where you don't have access?

Can you try the same code through Salesforce developer console once?
Mark CrooksyMark Crooksy
I'm using the same targetObjectId for both email attempts and the non-template route is working so I'm assuming I have access to that.
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 :(

 
Bhawani SharmaBhawani Sharma
This must help you: https://help.salesforce.com/HTViewHelpDoc?id=code_dev_console_execute_anonymous.htm&language=en_US#topic-title
Mark CrooksyMark Crooksy
Thanks for the link. And thank you for taking the time to help me here. I'm trying to get a simple email to be sent but not having any luck. Can you see what's incorrect with this code?

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();
Bhawani SharmaBhawani Sharma
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'bhavi@simplyforce.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Test');
mail.setPlainTextBody('Test');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Mark CrooksyMark Crooksy
Ok your code works fine and an email was sent.
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 });
Bhawani SharmaBhawani Sharma
Please follow this: http://salesforceondemand.blog.com/2012/01/27/to-send-email-from-apex-using-email-template/
This was selected as the best answer
Mark CrooksyMark Crooksy
Sorry about this!

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
Bhawani SharmaBhawani Sharma
Your query looks incorrect. Please make sure you have the correct name. Try below one. EmailTemplate et=[Select id from EmailTemplate where name= 'TestTemplate'];
Mark CrooksyMark Crooksy
Ok this code ran successfully. It seems that:
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 });
Bhawani SharmaBhawani Sharma
If you want to use the Email Template unique name in query, you will have to use DeveloperName field to query that. Select Id from EmailTemplate where DeveloperName = 'UniqueName'
Mark CrooksyMark Crooksy
OK, Got it thanks.
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";

:)