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
Big EarsBig Ears 

deploing email classes & methods from Sandbox to Production instances

Hey guys,

 

In our sandbox, I've created a Visualforce/Apex/email combo that provides users with a button to send an email to the owner of a record requesting ownership of that record. The button sits on the record page and, when pressed, the UI navigates to a new page where the user cancels or confirms the request. Upon confirmation, the controller calls a class/method that sends an email to the owner of the record (or an administrator, if the user is not active) requesting the transfer.

 

I'm unable to deploy the code to the production instance of our Salesforce. I believe this because I use the setTemplateId() method to inform the apex which template to use.

 

Is there a way to ensure the id of the deployed template matches the id of the template in the sandbox? Alternatively, is there a way for the apex to select a template based upon template name?

 

I cannot write the code in the production instance, as it has to have test coverage, surely?

 

How are we supposed to write apex that sends emails and uses templates? Am I being really stupid?

 

With thanks,

Andy

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

I solved a similar issue to this by using Custom Labels. Here is an example of what you can do.

 

Id templateId = Label.emailTemplateId; /**your code **/ .setTemplateId(this.templateId);

 Or you can query for the template Id like this.

 

final String EMAIL_TEMPLATE_NAME = Label.emailTemplateName; private Id getTemplateId(){ return [select Id from EmailTemplate where Name = : this.EMAIL_TEMPLATE_NAME].Id; } .setTemplateId(getTemplateId());

 

Please note that both examples are not production ready code and I would add a lot of error handling before deployment.

In the first solution you have to change the value of the email template Id after deployment, and in the second solution you don't have to do anything. Just make sure there is a template with the correct name.

 

 

 

 

All Answers

mikefmikef

I solved a similar issue to this by using Custom Labels. Here is an example of what you can do.

 

Id templateId = Label.emailTemplateId; /**your code **/ .setTemplateId(this.templateId);

 Or you can query for the template Id like this.

 

final String EMAIL_TEMPLATE_NAME = Label.emailTemplateName; private Id getTemplateId(){ return [select Id from EmailTemplate where Name = : this.EMAIL_TEMPLATE_NAME].Id; } .setTemplateId(getTemplateId());

 

Please note that both examples are not production ready code and I would add a lot of error handling before deployment.

In the first solution you have to change the value of the email template Id after deployment, and in the second solution you don't have to do anything. Just make sure there is a template with the correct name.

 

 

 

 

This was selected as the best answer
WesNolte__cWesNolte__c

Smart!

 

Wes 

Big EarsBig Ears

Thank you Mike,

 

I hadn't realised that you could run a query for email template objects like that. These are both two great solutions.

 

Andy