+ Start a Discussion
Stefano AmentaStefano Amenta 

Case Feed: change FromAddress value with Apex Class

Hi,

I have put together a class that should change the From Address in the case feed based on some criteria.

In the debug logs, I can see the class is returning the correct value when the criteria are met, but in the case feed the From Address is always set to my email address.

Do you know why the case feed doesn't take into consideration the criteria in my class?
 
global class DefaultEmailLoader implements QuickAction.QuickActionDefaultsHandler {
    
    global DefaultEmailLoader() {
    }    
    
    global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
        QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = null;
    
    System.debug(LoggingLevel.INFO, '*** : ' + defaults[0]);

        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.E_Mail_to_User_IT') && 
               defaults.get(j).getActionType().equals('SendEmail')) {
                   sendEmailDefaults = 
                       (QuickAction.SendEmailQuickActionDefaults)defaults.get(j);
                   break;
            }
        }
        
        if (sendEmailDefaults != null) {
            Case c = [SELECT Status, RecordTypeId, Creator_Office__c FROM Case 
                      WHERE Id=:sendEmailDefaults.getContextId()];
        
            EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();  
            emailMessage.FromAddress = getFromAddress(c.RecordTypeId, c.Creator_Office__c);
            system.debug('Record type= ' + c.RecordTypeId);
            system.debug('Office= ' + c.Creator_Office__c);

            System.debug(LoggingLevel.INFO, '*** emailMessage.FromAddress: ' + emailMessage.FromAddress);
            

            if (sendEmailDefaults.getInReplyToId() == null) {
                Integer emailCount = [SELECT count() FROM EmailMessage 
                                      WHERE ParentId=:sendEmailDefaults.getContextId()];
                if (emailCount!= null && emailCount > 0) {
                    sendEmailDefaults.setTemplateId(
                        getTemplateIdHelper('GIT_Send_e_mail_with_case_description_2')); 
                } else {
                    sendEmailDefaults.setTemplateId(
                        getTemplateIdHelper('GIT_Send_e_mail_with_case_description_1st'));
                }
                sendEmailDefaults.setInsertTemplateBody(false);
                sendEmailDefaults.setIgnoreTemplateSubject(false);
            } else {
                sendEmailDefaults.setTemplateId(
                    getTemplateIdHelper('E1_Case_escalation_notification_unassigned'));
                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);
        }
        return templateId;
    }
private String getFromAddress(Id recordType, String office) {
    System.debug(LoggingLevel.INFO, '*** : ' + recordType.equals('0120I0000019cdyQAA') + ' ' + office.contains('CN'));
    system.debug('Record type= ' + recordType);
    system.debug('Office= ' + office);
        if (recordType.equals('0120I0000019cdyQAA') && office.contains('CN')) 
            { return 'email1@example.com'; } 
        else if (recordType.equals('0120I0000019cdyQAA') && office.contains('RU')) 
            { return 'email2@example.com'; } 
        else if (recordType.equals('0120I0000019cdyQAA') && office.contains('ID')) 
            { return 'email3@example.com'; }
            else { return 'email4@example.com'; }
       
    }
}

Case Feed view: the FromAddress is always set to my name.

User-added image