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 🙂