Hey guys, today in this post we are going to learn about how to get specific value of custom label in apex class method in Lightning Component Salesforce.
Custom labels enable developers to create multilingual applications by automatically presenting information in a user’s native language. Custom labels are custom text values that can be accessed from Apex classes, Visualforce pages, Lightning pages, or Lightning components. The values can be translated into any language Salesforce supports.
You can create up to 5,000 custom labels for your organization, and they can be up to 1,000 characters in length. Custom labels from managed packages donβt count toward this limit.
To access custom labels, from Setup >> enter Custom Labels in the Quick Find box >> then select Custom Labels.
How you add a custom label to your application depends on the user interface. For more information on the following syntax, see the corresponding developer guides.
- In Apex use the System.Label.Label_name syntax.
- In Visualforce, use the $Label global variable.
- In Aura components, use the $Label.c.labelName syntax for the default namespace or $Label.namespace.labelName if your org has a namespace or to access a label in a managed package.
- In Lightning web components, import the label using the @salesforce/label/namespace.Label_name syntax.
- In Lightning App Builder component labels and attributes, use the {!$Label.customLabelName} expression.
β To know more details about Custom Labels, Click Here..
Files we used to fetch the dynamic custom label through apex class in Lightning Component β
dynamicCustomLabelCmp.cmp | Lightning Component | It is used to get the dynamic custom label through apex in lightning component |
dynamicCustomLabelCmpController.js | JavaScript Controller | It is hold Javascript Click function to fetch the custom label from server. |
dynamicCustomLabelCtrl.apxc | Apex Controller | It is used to get the custom label values from apex method. |
dynamicCustomLabelCmpApp | Lightning Application | It is used for call the component and preview on browser. |
Final Output β
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Lightning Component β
Step 1:- Create Lightning Component : dynamicCustomLabelCmp.cmp
From Developer Console >> File >> New >> Lightning Component
dynamicCustomLabelCmp.cmp [Lightning Component File]
<aura:component controller="dynamicCustomLabelCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="customLablVal" type="String"/>
<aura:attribute name="custmLblStg" type="boolean" default="true"/>
<div class="slds slds-m-around_large">
<aura:if isTrue="{!v.custmLblStg}">
<div class="slds-text-align--center slds-m-bottom_large">
<button class="slds-button slds-button--brand" onclick="{!c.customLabelHide}">Hide Custom Label</button>
</div>
<table class="slds-table slds-table--bordered slds-table_col-bordered" style="border:1px #ddd solid; font-size:15px;">
<thead>
<tr>
<th style="background:#eee; font-size:17px;">Custom Label Name</th>
<th style="background:#eee; font-size:17px;">Custom Label Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>w3web_Salesforce_Tutorial</td>
<td><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer">{!v.customLablVal}</a></td>
</tr>
</tbody>
</table>
<aura:set attribute="else">
<div class="slds-text-align--center slds-m-bottom_large">
<button class="slds-button slds-button--brand" onclick="{!c.customLabelShow}">Show Custom Label</button>
</div>
</aura:set>
</aura:if>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like β</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Create JavaScript Controller β
Step 2:- Create Lightning Component : dynamicCustomLabelCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
dynamicCustomLabelCmpController.js [JavaScript Controller]
({
doInit:function(component,event,helper){
var action = component.get("c.custoLabelItem");
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
var result = response.getReturnValue();
component.set("v.customLablVal",result);
}
});
$A.enqueueAction(action);
},
customLabelShow : function(component, event, helper) {
component.set("v.custmLblStg",true);
},
customLabelHide : function(component, event, helper) {
component.set("v.custmLblStg",false);
},
})
Create Apex Class Controller β
Step 3:- Create Apex Class : dynamicCustomLabelCtrl.apxc
From Developer Console >> File >> New >> Apex Class
dynamicCustomLabelCtrl.apxc [Apex Class Controller]
public class dynamicCustomLabelCtrl {
//how TO find a specific custom label IN apex IN lightning component
@AuraEnabled
public static String custoLabelItem(){
List<String> custmValParm = NEW List<String>();
String custmLbl = Label.w3web_Salesforce_Tutorial;
String custmLblMsg = String.format(custmLbl, custmValParm);
System.debug('CustomLabelValue' + custmLblMsg);
RETURN custmLblMsg;
}
}
Create Lightning Application β
Step 4:- Create Lightning Application : dynamicCustomLabelCmpApp.app
From Developer Console >> File >> New >> Lightning Application
dynamicCustomLabelCmpApp.app [Component Application File]
<aura:application extends="force:slds">
<c:dynamicCustomLabelCmp/>
</aura:application>
Further post that would you like to learn in Salesforce
Can you use custom labels in Apex?
Custom labels have a limit of 1,000 characters and can be accessed from an Apex class. To define custom labels, from Setup, in the Quick Find box, enter Custom Labels , and then select Custom Labels. In your Apex class, reference the label with the syntax System. Label.
What is custom label in Apex?
Custom labels are custom text values that can be accessed from Apex classes, Visualforce pages, Lightning pages, or Lightning components. The values can be translated into any language Salesforce supports.
What is the difference between custom setting and custom metadata?
Main difference between custom metadata and custom setting is that custom metadata records are deployable and packagable. But we can not deploy custom setting data. Custom settings enable you to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |