[SalesForce] How a Custom Metadata Type’s field value can be referred in Lightning Component

I have an URL that was saved in a Text(255) field of the Custom Metadata Type. I wanted to refer this Url on to the Lightning component, to make my custom onClick() to redirect to this Url. How can I pass this URL to window.location.href?

Do I need to query the field in an Apex Controller class using:

Select Id, UrlField from MyPortal_Configuration_Setting__mdt

If yes, how can implement in Component's JS controller?
If no, what is the valid workaround for referring URL from Custom Metadata type? Can someone take a look at this please?

Best Answer

To redirect to an url, if you're in Lightning Experience you should make use of the force:navigateToURL event.

To get the Custom Metadata Type, I'm afraid you'll have to query the server via an Apex Controller, yes. I tried to use Lightning Data Service, but unfortunately it doesn't work with Custom Metadata. But I would definitely keep an eye on it as it would make sense to support Custom Metadata Types in LDS in the future.

Related Topic