Contact Us:

670 Lafayette Ave, Brooklyn,
NY 11216

+1 800 966 4564
+1 800 9667 4558

How to use namespace in managed lightning components

If you are working on a lightning component and it’s going to be a part of a managed package then you need to take care of namespace in client side JS code if you are accessing any custom field. This can be handled in various ways but I found one very quick way to handle this. Using wrapper could be a best way but it’s time consuming so you can use the following way.

Get the namesapce from server side controller and use that in the lightning components.

NamespaceDemoComp.cmp

[gist id=928e51b8a0674c5aa4964cf621788e1d file=NamespaceDemoComp.html]

NamespaceDemoCompController.cls

[gist id=928e51b8a0674c5aa4964cf621788e1d file=NamespaceDemoCompController.cls]

NamespaceDemoCompController.js

[gist id=928e51b8a0674c5aa4964cf621788e1d file=NamespaceDemoCompController.js]

NamespaceDemoCompHelper.js

[gist id=928e51b8a0674c5aa4964cf621788e1d file=NamespaceDemoCompHelper.js]
So, you can see in the above example that using namespace in JS code can save lot of time as compared to wrapper classes. Expense__c is a custom object and this code will take care of the namespace automatically based on the running environment. If there is a namespace then it will be added as suffix with field’s API names in JS code.
Happy coding 🙂

Leave a comment

Your email address will not be published. Required fields are marked *