15

I need to get and use the field label in my lightning component.

In VF it is possible to do this via the:

{!$ObjectType.Account.fields.Title.Label} 

OR:

{!$ObjectType['Account'].fields['Title'].Label}

How can this be done in lightning component markup or controller?

Itai Shmida
  • 5,025
  • 12
  • 59
  • 86

3 Answers3

13

You need to call server side controller from lightning and use following code (Describe API) there:

Schema.SObjectField F = fieldNameMap.get(fieldAPIName); //where fieldAPIName is API name of field
Schema.DescribeFieldResult R = F.getDescribe();
String fieldLabel=R.getLabel();
return fieldLabel;

This is the only way as far as I know. Else, you can create new custom labels for field names labels and use them directly in lightning using "$Label.c.Custom_Label"

Sarang
  • 2,580
  • 8
  • 49
  • 98
  • I just state here that this answer is not what I am asking for - I want to get the labels from the client side and not from the server side. this is still not possible – Itai Shmida Nov 12 '17 at 16:09
11

Answer is:

This cannot be done in lightning component as of today!!!

please vote this idea to make this feature available sooner:

Access sObject field labels from lightning component

Guy Clairbois
  • 10,634
  • 31
  • 49
Itai Shmida
  • 5,025
  • 12
  • 59
  • 86
0

**Using below code you can get label for any field in LWC for particular object **

`import { LightningElement, wire, api, track } from 'lwc'
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import NAME_FIELD from '@salesforce/schema/opportunity.Name';

export default class LabelInMsg extends LightningElement {

data;
@wire(getObjectInfo, { objectApiName: OPPORTUNITY_OBJECT })
oppInfo({ data, error }) {
    this.data=data;
    if (data) {  console.log('result=> ', this.data.fields[NAME_FIELD.fieldApiName].label);}
}



}`