I have a class for setting a default ToAddress when using a specific email action. See below:

global class EmailHandler implements QuickAction.QuickActionDefaultsHandler {
    // Empty constructor  
    global EmailHandler() {  
    // 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
        // and set sendEmailDefaults 
        for (Integer j = 0; j < defaults.size(); j++) {  
            sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(j);  
            System.debug('Email Defaults For Loop ' + j + ': ' + sendEmailDefaults);

        if(sendEmailDefaults != null) {
            //select the relevant case
            Case c = [SELECT CaseNumber, Source__c, CC_Team__c, Subject, OwnerId 
                      FROM Case WHERE Id=:sendEmailDefaults.getContextId()]; 
            System.debug('Case ID: ' + c.Id);
            //select the user that owns the above case
            User u = [SELECT FirstName, LastName FROM User WHERE Id=:c.OwnerId];

            EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();

            //this will clear certain fields if using the centre console
            if (sendEmailDefaults.getActionName() == 'Case.CC_Send_Email') {
                emailMessage.ToAddress = '';
                emailMessage.CcAddress = '';
            System.debug('To Address after clean: ' + emailMessage);       

I am trying to write a test class for this which is shown below:

private class EmailHandlerTest {
    static Case myCase {get;set;}
    static EmailMessage myMsg {get;set;}

    static testmethod void EmailPublisherLoader_NoReplyToId() {
        Exception failureDuringExecution = null;

        //create QuickActionDefaults
        List<Map<String, Object>> defaultSettingAsObject = new List<Map<String, Object>>
            new Map<String, Object>
                'targetSObject' => new EmailMessage(),
                    'contextId' => myCase.Id,
                    'ToAddress' => myMsg.ToAddress,
                    'CCAddress' => myMsg.CcAddress,
                    'actionType' => 'Email',
                    'actionName' => 'Case.CC_Send_Email',
                    'fromAddressList' => new List<String> { 'salesforce@test.com' }

            List<QuickAction.SendEmailQuickActionDefaults> defaultsSettings = 
            (List<QuickAction.SendEmailQuickActionDefaults>)JSON.deserialize(JSON.serialize(defaultSettingAsObject), List<QuickAction.SendEmailQuickActionDefaults>.class);

        (new EmailPublisherLoader()).onInitDefaults(defaultsSettings);
        System.debug('defaultsSettings after: ' + defaultsSettings);
        //System.assertEquals(null, defaultsSettings.QuickActionDefaults.targetSObject.ToAddress);  

    static void init(){
        myCase = 
            new Case(
                Priority = 'Medium',
                Origin = 'Email',
                RecordTypeId = '012800000007qIgAAI'
        insert myCase;

        myMsg = 
            new EmailMessage(
                ToAddress = 'ToAddress@test.com',
                CCAddress = 'CCAddress@test.com',
                ParentId = myCase.Id
        insert myMsg;

My problem is that while my test class seems to be working and my debug statement of System.debug('defaultsSettings after: ' + defaultsSettings); shows me that ToAddress has been blanked, I am not sure how to verify this using System.assertEquals() because I don't know how to access to value I need. Here is the output of that debug statement:

10:27:36:053 USER_DEBUG [35]|DEBUG|defaultsSettings after: (SendEmailQuickActionDefaults:[QuickActionDefaults.actionName=Case.CC_Send_Email, QuickActionDefaults.actionType=Email, QuickActionDefaults.contextId=50023000001yVHlAAM, QuickActionDefaults.targetSObject=EmailMessage:{ToAddress=}, emailTemplateId=null, fromAddressList=(), ignoreTemplateSubject=null, insertTemplateBody=null, replyToId=null]) 

My question is how do I access the ToAddress from the defaultsSettings list to check it is null for my test?


Disclaimer: I'm not very familiar with the functionality of QuickAction.QuickActionDefaultsHandler.

I was able to get your test class to pass with some additional assertions by doing this:

    System.assertEquals(null, ((EmailMessage)defaultsSettings[0].getTargetSObject()).ToAddress);  
    (new EmailHandler()).onInitDefaults(defaultsSettings);
    System.assertEquals('', ((EmailMessage)defaultsSettings[0].getTargetSObject()).ToAddress);  

The ToAddress of your EmailMessage starts out as null (it's simply not populated in test context), and that's what your main class alters. Since the main class populates an empty string, we can detect the change.

  • Thanks David, I was able to get the test to work as well by doing something similar. – Luke Bray Jun 14 at 14:00

I was able to get this to work by doing the following:

    //Do the tests
    (new EmailHandler()).onInitDefaults(defaultsSettings);
    System.debug('defaultsSettings after: ' + defaultsSettings);

    System.assertEquals('', defaultsSettings.get(0).getTargetSObject().get(EmailMessage.ToAddress));
    System.assertEquals('', defaultsSettings.get(0).getTargetSObject().get(EmailMessage.CcAddress));

I hadn't realised that defaultsSettings was actually a list rather than the settings themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.