I have a constants class that has:

public static final String DATA_TYPE_INTEGER = 'INTEGER';

I tried to access this in my LWC by doing an import like this:

import DATA_TYPE_INTEGER from '@salesforce/apex/ConstantsClass.DATA_TYPE_INTEGER';

But in my Javascript, .toLowerCase() is not an operation on DATA_TYPE_INTEGER and I can't seem to get any actual string value from it.

I then tried in my apex class for my LWC:

public final String DATA_TYPE_INTEGER = ConstantsClass.DATA_TYPE_INTEGER;

And importing that, but got the same issues. I don't think Lightning Components allowed you to access variables like this at all, but was thinking that LWCs might, like they can import custom labels...

Is this actually possible? And if so, how do I go about importing an apex class variable into my javascript?

  • What is your use case, why would you need to export a constant from Apex into JS?
    – Jayant Das
    Mar 25, 2019 at 16:04
  • We actually should change the 'INTEGER' to Integer.ObjectType or however we might get that. We do not want hard coded strings thrown around the code, we want them contained and rely on returned Salesforce information (like with Object.SObjectType()) to help with translation. So looking to get the value out of Apex to get the type, or directly get the type from some kind of import LWCs support. Like, can you just directly import type names? Mar 25, 2019 at 16:09
  • Writing import does not import the value, it only imports the metadata for calling that method or property. Mar 25, 2019 at 17:23

1 Answer 1


There's no way at least of today to import constant values directly in JS from Apex. The only available way is to import Apex methods. You can actually utilize this for your use case here.

What you can do is to declare an apex method which returns your constant class ConstantsClass, and utilize it on your JS. This will in fact help you to get all the constant values in a single call. Sample below.


public final String DATA_TYPE_INTEGER = 'INTEGER';


public static ConstantClass getAllConstants() {
    return new ConstantClass();

JS Controller

import getConstants from '@salesforce/apex/MyApexController.getAllConstants';


allConstants ({error, data}) {
    if (data) {
        console.log(data.DATA_TYPE_INTEGER ); // will give you the value of your constant
    } else {
        this.error = error;
  • 1
    This is fantastic, I never put 1 and 1 together but after seeing it typed out it was obvious we can do this
    – tsalb
    Mar 25, 2019 at 22:22
  • @tsalb I thought why not use what's available for this use case :)
    – Jayant Das
    Mar 26, 2019 at 1:13
  • 1
    Sorry for the late reply, but this is a clever way to handle this :). Thank you. Mar 29, 2019 at 15:07
  • @TylerDahle Glad that it worked for you :)
    – Jayant Das
    Mar 29, 2019 at 15:08
  • Great!, but does it have side effects on the performance & overall user experience as we are importing a big class (let's say 400 constants) in the client or it'll be just a minor effect @JayantDas Aug 27, 2023 at 15:23

You must log in to answer this question.

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