Newer Version Available
QuickActionDefaultsHandler Interface
Namespace
Usage
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.
- The interface overrides email values set up with predefined IDs.
- The interface works with the out-of-the-box Email action provided on cases. You can also use the interface with custom Email actions for the case object.
- The interface in Lightning Experience doesn’t support:
- Email attachments
- Custom email fields
- Visualforce email templates, which are a type of email template available in Salesforce Classic
- The From Address picklist isn’t customizable in Send Email action types via the QuickActionDefaultsHandler Interface.
- If your Apex interface adds content to the email body, merge fields display as unresolved. During preview and send, the merge fields resolve.
When you implement this interface, provide an empty parameterless constructor.
QuickActionDefaultsHandler Methods
The following are methods for QuickActionDefaultsHandler.
onInitDefaults(actionDefaults)
Signature
public void onInitDefaults(QuickAction.QuickActionDefaults[] actionDefaults)
Parameters
- actionDefaults
- Type: QuickAction.QuickActionDefaults[]
- This array contains only one item of type QuickAction.SendEmailQuickActionDefaults.
Return Value
Type: void
QuickActionDefaultsHandler Example Implementations
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.
1global class EmailPublisherLoader implements QuickAction.QuickActionDefaultsHandler {
2 // Empty constructor
3 global EmailPublisherLoader() {
4 }
5
6 // The main interface method
7 global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
8 QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = null;
9
10
11 // Check if the quick action is the standard case feed Send Email action
12 for (Integer j = 0; j < defaults.size(); j++) {
13 if (defaults.get(j) instanceof QuickAction.SendEmailQuickActionDefaults &&
14 defaults.get(j).getTargetSObject().getSObjectType() ==
15 EmailMessage.sObjectType &&
16 defaults.get(j).getActionName().equals('Case.Email') &&
17 defaults.get(j).getActionType().equals('Email')) {
18 sendEmailDefaults =
19 (QuickAction.SendEmailQuickActionDefaults)defaults.get(j);
20 break;
21 }
22 }
23
24 if (sendEmailDefaults != null) {
25 Case c = [SELECT Status, Reason FROM Case
26 WHERE Id=:sendEmailDefaults.getContextId()];
27
28 EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();
29 // Set BCC address to make sure each email goes for audit
30 emailMessage.BccAddress = getBccAddress(c.Reason);
31
32 /*
33 Set Template related fields
34 when the In Reply To Id field is null we know the interface
35 is called on page load. Here we check if
36 there are any previous emails attached to the case and load
37 the 'New_Case_Created' or 'Automatic_Response' template.
38 When the In Reply To Id field is not null we know that
39 the interface is called on click of reply/reply all
40 of an email and we load the 'Default_reply_template' template
41 */
42 if (sendEmailDefaults.getInReplyToId() == null) {
43 Integer emailCount = [SELECT count() FROM EmailMessage
44 WHERE ParentId=:sendEmailDefaults.getContextId()];
45 if (emailCount!= null && emailCount > 0) {
46 sendEmailDefaults.setTemplateId(
47 getTemplateIdHelper('Automatic_Response'));
48 } else {
49 sendEmailDefaults.setTemplateId(
50 getTemplateIdHelper('New_Case_Created'));
51 }
52 sendEmailDefaults.setInsertTemplateBody(false);
53 sendEmailDefaults.setIgnoreTemplateSubject(false);
54 } else {
55 sendEmailDefaults.setTemplateId(
56 getTemplateIdHelper('Default_reply_template'));
57 sendEmailDefaults.setInsertTemplateBody(false);
58 sendEmailDefaults.setIgnoreTemplateSubject(true);
59 }
60 }
61 }
62
63 private Id getTemplateIdHelper(String templateApiName) {
64 Id templateId = null;
65 try {
66 templateId = [select id, name from EmailTemplate
67 where developername = : templateApiName].id;
68 } catch (Exception e) {
69 system.debug('Unble to locate EmailTemplate using name: ' +
70 templateApiName + ' refer to Setup | Communications Templates '
71 + templateApiName);
72 }
73 return templateId;
74 }
75private String getBccAddress(String reason) {
76 if (reason != null && reason.equals('Technical'))
77 { return 'support_technical@mycompany.com'; }
78 else if (reason != null && reason.equals('Billing'))
79 { return 'support_billing@mycompany.com'; }
80 else { return 'support@mycompany.com'; }
81 }
82
83
84}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.
1global class EmailPublisherForHighPriorityCases implements QuickAction.QuickActionDefaultsHandler {
2 // Empty constructor
3 global EmailPublisherForHighPriorityCases() {
4 }
5
6 // The main interface method
7 global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
8 QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0);
9 EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();
10
11 Case c = [SELECT CaseNumber, Priority FROM Case WHERE Id=:sendEmailDefaults.getContextId()];
12
13 // If case severity is “High,” append “managers@acme.com” to the existing (and possibly blank) BCC field
14 if (c.Priority != null && c.Priority.equals('High')) { // Priority is 'High'
15 emailMessage.BccAddress = 'managers@acme.com';
16 }
17 }
18}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.
1global class EmailPublisherForCaseType implements QuickAction.QuickActionDefaultsHandler {
2 // Empty constructor
3 global EmailPublisherForCaseType() {
4 }
5
6 // The main interface method
7 global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
8 QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0);
9 EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();
10
11 Case c = [SELECT CaseNumber, Type FROM Case WHERE Id=:sendEmailDefaults.getContextId()];
12
13 // If case type is “Problem,” insert the “First Response” email template
14 if (c.CaseNumber != null && c.Type.equals('Problem')) {
15 sendEmailDefaults.setTemplateId('Insert Email Template ID Here'); // Set the template Id corresponding to First Response
16 sendEmailDefaults.setInsertTemplateBody(true);
17 sendEmailDefaults.setIgnoreTemplateSubject(false);
18 }
19}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.
1global class EmailPublisherForReplyAndReplyAll implements QuickAction.QuickActionDefaultsHandler {
2
3 // Empty constructor
4 global EmailPublisherForReplyAndReplyAll() {
5
6 }
7
8 // The main interface method
9 global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
10
11 QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0);
12 EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();
13
14 // If the email is a “Reply” email, insert the “Reply Email Template” to the email body
15 if (sendEmailDefaults.getActionName().equals('EmailMessage._Reply')) {
16 sendEmailDefaults.setTemplateId('Insert Reply Email Template ID Here');
17
18 sendEmailDefaults.setInsertTemplateBody(true);
19 sendEmailDefaults.setIgnoreTemplateSubject(false);
20
21 // If the email is a “Reply All” email, insert the “Reply All Email Template” to the email body
22 } else if (sendEmailDefaults.getActionName().equals('EmailMessage._ReplyAll')) {
23 sendEmailDefaults.setTemplateId('Insert Reply All Email Template ID Here');
24
25 sendEmailDefaults.setInsertTemplateBody(true);
26 sendEmailDefaults.setIgnoreTemplateSubject(false);
27
28}