To specify default values for the standard Email action in the case feed, create a class that implements QuickAction.QuickActionDefaultsHandler.
The QuickAction.QuickActionDefaultsHandler interface works in Salesforce Classic and Lightning Experience.
When you implement this interface, provide an empty parameterless constructor.
The following are methods for QuickActionDefaultsHandler.
public void onInitDefaults(QuickAction.QuickActionDefaults[] actionDefaults)
Type: void
These examples are implementations of the QuickAction.QuickActionDefaultsHandler interface.
In this example, the onInitDefaults method checks whether the element passed in the array is for the standard Email action in the case feed. Then, it performs a query to retrieve the case that corresponds to the context ID. Next, it sets the value of the BCC address of the corresponding email message to a default value. The default value is based on the case reason. Finally, it sets the default values of the email template properties. The onInitDefaults method determines the default values based on two criteria: first, whether a reply action on an email message initiated the call to the method, and second, whether any previous emails attached to the case are associated with the call.
global class EmailPublisherLoader implements QuickAction.QuickActionDefaultsHandler { // Empty constructor global EmailPublisherLoader() { } // The main interface method global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) { QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = null; // Check if the quick action is the standard case feed Send Email action for (Integer j = 0; j < defaults.size(); j++) { if (defaults.get(j) instanceof QuickAction.SendEmailQuickActionDefaults && defaults.get(j).getTargetSObject().getSObjectType() == EmailMessage.sObjectType && defaults.get(j).getActionName().equals('Case.Email') && defaults.get(j).getActionType().equals('Email')) { sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(j); break; } } if (sendEmailDefaults != null) { Case c = [SELECT Status, Reason FROM Case WHERE Id=:sendEmailDefaults.getContextId()]; EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject(); // Set BCC address to make sure each email goes for audit emailMessage.BccAddress = getBccAddress(c.Reason); /* Set Template related fields when the In Reply To Id field is null we know the interface is called on page load. Here we check if there are any previous emails attached to the case and load the 'New_Case_Created' or 'Automatic_Response' template. When the In Reply To Id field is not null we know that the interface is called on click of reply/reply all of an email and we load the 'Default_reply_template' template */ if (sendEmailDefaults.getInReplyToId() == null) { Integer emailCount = [SELECT count() FROM EmailMessage WHERE ParentId=:sendEmailDefaults.getContextId()]; if (emailCount!= null && emailCount > 0) { sendEmailDefaults.setTemplateId( getTemplateIdHelper('Automatic_Response')); } else { sendEmailDefaults.setTemplateId( getTemplateIdHelper('New_Case_Created')); } sendEmailDefaults.setInsertTemplateBody(false); sendEmailDefaults.setIgnoreTemplateSubject(false); } else { sendEmailDefaults.setTemplateId( getTemplateIdHelper('Default_reply_template')); sendEmailDefaults.setInsertTemplateBody(false); sendEmailDefaults.setIgnoreTemplateSubject(true); } } } private Id getTemplateIdHelper(String templateApiName) { Id templateId = null; try { templateId = [select id, name from EmailTemplate where developername = : templateApiName].id; } catch (Exception e) { system.debug('Unble to locate EmailTemplate using name: ' + templateApiName + ' refer to Setup | Communications Templates ' + templateApiName); } return templateId; } private String getBccAddress(String reason) { if (reason != null && reason.equals('Technical')) { return 'support_technical@mycompany.com'; } else if (reason != null && reason.equals('Billing')) { return 'support_billing@mycompany.com'; } else { return 'support@mycompany.com'; } } }
In this example, the onInitDefaults method checks whether the element passed in the array is for the standard Email action in the case feed. Then it performs a query to determine if the case Priority is set to High. If the Priority is set to High, the email address managers@acme.com is appended to the BCC field.
global class EmailPublisherForHighPriorityCases implements QuickAction.QuickActionDefaultsHandler { // Empty constructor global EmailPublisherForHighPriorityCases() { } // The main interface method global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) { QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0); EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject(); Case c = [SELECT CaseNumber, Priority FROM Case WHERE Id=:sendEmailDefaults.getContextId()]; // If case severity is “High,” append “managers@acme.com” to the existing (and possibly blank) BCC field if (c.Priority != null && c.Priority.equals('High')) { // Priority is 'High' emailMessage.BccAddress = 'managers@acme.com'; } } }
In this example, the onInitDefaults method checks whether the element passed in the array is for the standard Email action in the case feed. Then it performs a query to determine if the case Type is set to Problem. If the type is set to Problem, the First Response email template is inserted into the body of the email.
global class EmailPublisherForCaseType implements QuickAction.QuickActionDefaultsHandler { // Empty constructor global EmailPublisherForCaseType() { } // The main interface method global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) { QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0); EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject(); Case c = [SELECT CaseNumber, Type FROM Case WHERE Id=:sendEmailDefaults.getContextId()]; // If case type is “Problem,” insert the “First Response” email template if (c.CaseNumber != null && c.Type.equals('Problem')) { sendEmailDefaults.setTemplateId('Insert Email Template ID Here'); // Set the template Id corresponding to First Response sendEmailDefaults.setInsertTemplateBody(true); sendEmailDefaults.setIgnoreTemplateSubject(false); } }
In this example, the onInitDefaults method checks whether the element passed in the array is for the standard Email action in the case feed. Then it performs a query to determine if the email is a Reply or Reply All email. If email is a Reply or Reply All email, the corresponding email templates for these emails are inserted into the body of the email.
global class EmailPublisherForReplyAndReplyAll implements QuickAction.QuickActionDefaultsHandler { // Empty constructor global EmailPublisherForReplyAndReplyAll() { } // The main interface method global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) { QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0); EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject(); // If the email is a “Reply” email, insert the “Reply Email Template” to the email body if (sendEmailDefaults.getActionName().equals('EmailMessage._Reply')) { sendEmailDefaults.setTemplateId('Insert Reply Email Template ID Here'); sendEmailDefaults.setInsertTemplateBody(true); sendEmailDefaults.setIgnoreTemplateSubject(false); // If the email is a “Reply All” email, insert the “Reply All Email Template” to the email body } else if (sendEmailDefaults.getActionName().equals('EmailMessage._ReplyAll')) { sendEmailDefaults.setTemplateId('Insert Reply All Email Template ID Here'); sendEmailDefaults.setInsertTemplateBody(true); sendEmailDefaults.setIgnoreTemplateSubject(false); }