4

When running the following method the final debug shows that only the fatal severity messages are included. The other debugs show that it does see the other severities and does at least attempt to add them.

If I add something like if(sev != ApexPages.severy.FATAL), the next debug will show only warnings and so on.

private void createTestMessages() {
    for (ApexPages.Severity sev : ApexPages.Severity.values()) {
        system.debug(sev.name());
        for (integer i = 0; i < 2; i++) {
            system.debug(i);
            ApexPages.addMessage(new ApexPages.message(
                sev,
                'Message ' + i
            ));
        }
    }

    system.debug(ApexPages.getMessages());
}
4

Yes, you can use all five severity values at once. The problem you ran in to is actually a different issue. Each ApexPages.Message is deduplicated based on the message value, regardless of severity. A slight change to your code results in the output you're looking for:

private void createTestMessages() {
    for (ApexPages.Severity sev : ApexPages.Severity.values()) {
        system.debug(sev.name());
        for (integer i = 0; i < 2; i++) {
            system.debug(i);
            ApexPages.addMessage(new ApexPages.message(
                sev,
                sev+' Message ' + i
            ));
        }
    }

    system.debug(ApexPages.getMessages());
}

Here, we modify the message to make each one unique (severity + index), and the result is the 10 messages you'd expect.

13:59:22:025 USER_DEBUG [12]|DEBUG|(ApexPages.Message["FATAL Message 0"], ApexPages.Message["FATAL Message 1"], ApexPages.Message["ERROR Message 0"], ApexPages.Message["ERROR Message 1"], ApexPages.Message["WARNING Message 0"], ApexPages.Message["WARNING Message 1"], ApexPages.Message["INFO Message 0"], ApexPages.Message["INFO Message 1"], ApexPages.Message["CONFIRM Message 0"], ApexPages.Message["CONFIRM Message 1"])

| improve this answer | |

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.