0

I have a custom data type, 'CaseOutput' that returns a list of case records. The below system.debug returns the first CaseOutput element.

system.debug(CaseEinsteinHandler.getCaseStatus(listInput)[0]);

yields

USER_DEBUG [20]|DEBUG|CaseOutput:[cases=(Case:{CaseNumber=00101778, Status=Open, Id=5007A000004gm9wQAA})]

I would like to access CaseNumber to use for my System.assertEquals comparison. How can I extract that value?

Apex Class

public with sharing class CaseEinsteinHandler {

    /**************************************/
    public class CaseOutput {
        @InvocableVariable(required=true)
        public List <Case> cases;
    }

    public class CaseInput {
        @InvocableVariable(required=true)
        public String sEmailAddress;
    }
    /**************************************/

    @InvocableMethod(label='Get Case Status')
    public static List<CaseOutput> getCaseStatus(List<CaseInput> caseInputs) {
        // Build a list of case status values for the output
        List<CaseOutput> caseOutputs = new List<CaseOutput>();

        // Get the case objects (records) from the set of contact email addresses
        if (caseInputs.size() > 0 && caseInputs[0].sEmailAddress != null) {
            List<Case> allCases = 
                [SELECT CaseNumber, Status, Subject
                    FROM Case WHERE ContactEmail = :caseInputs[0].sEmailAddress AND Status In ('Open', 'In Progress', 'Escalated')]; 
            CaseOutput caseOutput = new CaseOutput();
            caseOutput.cases = allCases;
            caseOutputs.add(caseOutput);             
        }

        return caseOutputs;    
    }
}

Unit Test

@isTest private class CaseEinsteinHandlerTest {
    @testSetup
    static void setupTestData() {
        Account a = new Account(Name = 'Test Account');
        insert a;
        Contact ct = new Contact(LastName = 'TestContact', AccountId = a.id, Email = '[email protected]');
        insert ct;
        Case c = new Case(Status='Open', Origin = 'Email', ContactId = ct.Id);
        insert c;
    }
    static TestMethod void test_searchCase() {

        CaseEinsteinHandler.CaseInput input = new CaseEinsteinHandler.CaseInput();
        input.sEmailAddress = '[email protected]';

        List<CaseEinsteinHandler.CaseInput> listInput = new List<CaseEinsteinHandler.CaseInput>();
        listInput.add(input);

        // need to drill down 
        system.debug(CaseEinsteinHandler.getCaseStatus(listInput)[0]);

        // System.assertEquals();
    }
}

1 Answer 1

0
insert c;
c = [SELECT CaseNumber FROM Case WHERE Id = :c.Id];  // CaseNumber avail only on query

...


List<CaseEinsteinHandler.CaseOutput> caseOutputs = CaseEinsteinHandler.getCaseStatus(listInput);

List<Case> cases = caseOutputs[0].cases;

System.assertEquals(c.CaseNumber,cases[0].CaseNumber,'invocable should find mocked Case');

You can collapse the last three lines into:

System.assertEquals(c.CaseNumber,
                    CaseEinsteinHandler.getCaseStatus(listInput)[0].cases[0].CaseNumber,
                    'invocable should find mocked Case');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .