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 '19 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? – Tyler Dahle Mar 25 '19 at 16:09
  • Writing import does not import the value, it only imports the metadata for calling that method or property. – Pranay Jaiswal Mar 25 '19 at 17:23

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 '19 at 22:22
  • @tsalb I thought why not use what's available for this use case :) – Jayant Das Mar 26 '19 at 1:13
  • 1
    Sorry for the late reply, but this is a clever way to handle this :). Thank you. – Tyler Dahle Mar 29 '19 at 15:07
  • @TylerDahle Glad that it worked for you :) – Jayant Das Mar 29 '19 at 15:08

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.