Email template updates in managed packages don’t automatically reflect after upgrades. Discover how to ensure your changes successfully propagate across all customer orgs.
Leverage this Apex class to programmatically update email templates and ensure your changes take effect seamlessly after package upgrades. You can include this class as part of your managed package and invoke it via a button click in the subscriber org, or run it directly in the Anonymous Apex window for quick updates.
public with sharing class EmailTemplateHttpService {
public static void patchEmailTemplate() {
Id templateId = [
SELECT Id FROM EmailTemplate
WHERE Name = 'TEMPLATE-NAME'
LIMIT 1
].Id;
String htmlBody = '{ "HtmlValue": "Hello Example,...",
"subject": "Subject" }';
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getOrgDomainURL().toExternalForm() +
'/services/data/v62.0/sobjects/EmailTemplate/' +
String.valueOf(templateId));
req.setMethod('PATCH');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setBody(htmlBody);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300)
System.debug('Template updated successfully');
}
}
EmailTemplate object is required.