[SalesForce] the most efficient way to get object field labels in Lightning component

I need to display custom object field inputs on Lightning component. This has to support localization.

My code looks like this:

<aura:attribute name="myFieldsMap" type="Map" default="{ 'Field1__c': 'Field 1 label', 'Field2__c': 'Field 2 label',  }" />
<aura:attribute name="myObject" type="myObject__c" />

<lightning:input label="{!v.myFieldsMap.Field1__c}" value="v.myObject.Field1__c" />
<lightning:input label="{!v.myFieldsMap.Field2__c}" value="v.myObject.Field2__c" />

I get labels in apex using DescribeFieldResult and return values to component.

force:inputField is not rendered for some reason and I don't want to investigate why (Lightning Component – force:inputField not rendering)

What I don't like in this solution is that I have to specify default value for map with all fields I am using, because if label value is empty, component throws an error. For adding new fields I just want to populate lightning:input. Unfortunately I also have to add field definition to list in Apex to get the label, but specifying default makes this messy. Is there more elegant way than this?

Best Answer

Best practice and solution would be to get the actual sObject from the server side and put it as the attribute, lets say this is a custom object called TestObj__c:

<aura:attribute name="myObject" type="TestObj__c" />

Then - all fields that you will attach to components would have labels - also the translated ones!!!

And also - since getting field labels from client side is not supported yet - you can support my idea that does exactly what you have asked for:

https://success.salesforce.com/ideaView?id=0873A000000cNLeQAM

Related Topic