0

I have a service class with some error types:

public with sharing class ServiceClass {
    public enum ErrorType { ERROR }
}

And a controller method:

@AuraEnabled(cacheable=true)
public static ServiceClass getErrorTypes() {
    return new ServiceClass();
}

So far, when I load this class in my LWC, the class is an empty object.

When I try to annotate the enum to make it visible:

public with sharing class ServiceClass {
    @AuraEnabled
    public enum ErrorType { ERROR }
}

I get the error on deployment:

AuraEnabled fields cannot be static

I'm not explicitly making this enum static. How can I get enum values from a class to an LWC or bypass this error?

2

It's impossible to send enum values to LWCs as is, which is one of many undocumented Salesforce developer limitations. Instead, you can build a controller method that turns the enum values into a map, for use on the front end.

This is the code I ended up writing before others answered this question:

// It's impossible to serve enums verbatim to the front end, so convert it
// to a map instead
@AuraEnabled(cacheable=true)
public static Map<String, Integer> getErrorTypes() {
    Map<String, Integer> ordinals = new Map<String, Integer>();
    for(EnumType errorType : EnumType.values()) {
        ordinals.put(errorType.name(), errorType.ordinal());
    }
    return ordinals;
}

This way the front end can look up the enum values by the same name

import getErrorTypes from '@salesforce/apex/Controller.getErrorTypes';

...

this.errorTypes = await getErrorTypes(); // { ERROR_NAME: 0, ... }
| improve this answer | |
0

Given code describes a definition of an enum and does not define any properties of it.

In order to return it, you will need to define it as an property of given class. Check the following code:

public with sharing class ServiceClass {

    public enum ErrorType { ERROR }

    @AuraEnabled
    public ErrorType errorTypeProperty;
}

In that case, class will have one property, with type ErrorType and name errorTypeProperty.

| improve this answer | |
  • I'm trying to read the full enum set in the LWC, not read a specific enum value. This doesn't appear to solve that problem – Andy Ray Sep 18 at 22:02
  • I am afraid it would not be possible, since only properties, and not definitions are transferred to LWC. I think the best way to achieve desired result would be to create an array (or dictionary) with all enum values as a property and use that one on client side – kurunve Sep 19 at 6:14
0

As per @kurunve's suggestion to get the enumeration literals, as strings, to the LWC you simply need to do something like:

public class ServiceClass {
    public enum ErrorType { ERROR }

    @AuraEnabled(Cacheable=true)
    public static String[] getErrorTypes() {
        String[] result = new String[0];

        for (ErrorType e : ErrorType.values()) {
            result.add(String.valueOf(e));
        }

        return result;
    }
}

Note that whilst it is possible to have a method like:

    @AuraEnabled(Cacheable=true)
    public static ErrorType[] getErrorTypes() {
        return ErrorType.values();
    }

I was unable to see how to utilize the values received in the LWC.

If you want to be able to convert the enum literals as strings back to enum literals, take a look at this answer. This would allow you to handle sending error type values back to the server side and then treating them as an enum value.

| improve this answer | |
  • This is close but uses strings instead of integers, and doesn't include the enum names – Andy Ray Sep 30 at 19:52
  • There is nothing to say you need to use integers in the question, but if you prefer your approach, that's fine with me. – Phil W Sep 30 at 20:33

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.