I have a running implementation of a customized Email Publisher in the Case Feed, which implement QuickActionDefaultHandler to pre-populate Email Templates based on my Cases Record Type. Below is the Apex Class:
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, contact.Email, Additional_To__c, Additional_CC__c, Additional_BCC__c, RecordType.name FROM Case WHERE Id=:sendEmailDefaults.getContextId()];
EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();
//set TO address
if (c.contact.Email == c.Additional_To__c){
emailMessage.toAddress = (c.contact.Email);
}
else{
if (c.Additional_To__c != null){
emailMessage.toAddress = (c.contact.Email+' '+c.Additional_To__c);
}
}
In addition, I am setting the TO address according a combination of the Case Contact and the custom field - 'Additional_To__c'. The above solution is setting the TO properly, but when the Case Contact does exist in the 'Additional_To__c' as follows:
The outcome is a duplicity of the Case Contact - '[email protected]':