Apex is supposed to be based on Java which supports factory method in enums types.

global enum CheckResult {

    public static CheckResult fromResult(Boolean aBoolean) {
        if (aBoolean) {
            return PASSED;
        } else {
            return FAILED;

I cannot find a form that actually compiles, is this a supported feature in apex, if so how is it achieved in apex?


Apex does support enum's, syntax is bit different than java though.

Proper class based on your example would be as:

public class CheckResult {
    public Enum Result { PENDING, PASSED, FAILED}

    public static Result fromResult(Boolean aBoolean) {
        if (aBoolean) {
            return Result.PASSED;
        } else {
            return Result.FAILED;

You could verify the result from execute anonymous from developer console:

CheckResult.Result result = CheckResult.fromResult(true);
// this would be equal to enum value passed
System.debug(result == CheckResult.Result.PASSED);
  • That is great, I have several external checks to be made via REST that returns true or false results via JSON, each exist in salesforce as pending when the record is created but passed/failed as the process completes Your method will work perfectly since I wanted to avoid the conditional testing all over the code base. – Martin of Hessle May 28 '19 at 15:04
  • 1
    simplest code: return aBoolean ? Result.PASSED : Result.FAILED; – cropredy May 29 '19 at 1:17
  • @cropredy I value clarity over brevity – Martin of Hessle May 29 '19 at 10:24
  • @MartinofHessle - perfectly understandable. I was like you when I started with Apex but over time certain idioms became natural and I learned to value terseness (fewer lines). – cropredy May 29 '19 at 13:59

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.