how to get specific value of custom label in apex class method in Lightning Component Salesforce | how to access dynamic custom label in lightning component through apex class in Salesforce

4,571 views

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.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

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 →

get specific value of custom label in apex -- w3web.net

 

You can download file directly from github by Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

 

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]

  1.   <aura:component controller="dynamicCustomLabelCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2.  
  3.     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  4.     <aura:attribute name="customLablVal" type="String"/>
  5.     <aura:attribute name="custmLblStg" type="boolean" default="true"/>
  6.  
  7.     <div class="slds slds-m-around_large">
  8.  
  9.         <aura:if isTrue="{!v.custmLblStg}">
  10.             <div class="slds-text-align--center slds-m-bottom_large">
  11.                 <button class="slds-button slds-button--brand" onclick="{!c.customLabelHide}">Hide Custom Label</button>                
  12.             </div>
  13.             <table class="slds-table slds-table--bordered slds-table_col-bordered" style="border:1px #ddd solid; font-size:15px;">  
  14.                 <thead>  
  15.                     <tr>  
  16.                         <th style="background:#eee; font-size:17px;">Custom Label Name</th>
  17.                         <th style="background:#eee; font-size:17px;">Custom Label Value</th>
  18.                     </tr>  
  19.                 </thead>  
  20.                 <tbody>
  21.                     <tr>  
  22.                         <td>w3web_Salesforce_Tutorial</td>
  23.                         <td><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer">{!v.customLablVal}</a></td>
  24.                     </tr>   
  25.                 </tbody>  
  26.             </table> 
  27.             <aura:set attribute="else">
  28.                 <div class="slds-text-align--center slds-m-bottom_large">      
  29.                     <button class="slds-button slds-button--brand" onclick="{!c.customLabelShow}">Show Custom Label</button>                   
  30.                 </div>
  31.             </aura:set>
  32.         </aura:if>
  33.  
  34.         <!--Start RelatedTopics Section-->
  35. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  36.  
  37.     <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>
  38.  
  39.             <br/><br/>
  40.         <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>
  41.             <div style="display:block; overflow:hidden;"> 
  42.                 <div style="width: 50%; float:left; display:inline-block">
  43.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  44.                         <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>
  45.                         <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>
  46.                         <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>
  47.                         <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>
  48.                         <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>
  49.                     </ul>
  50.             </div>
  51.  
  52.             <div style="width: 50%; float:left; display:inline-block">
  53.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  54.                         <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>
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                         <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>
  59.                     </ul>
  60.                 </div>
  61.                <div style="clear:both;"></div> 
  62.                <br/>
  63.                 <div class="youtubeIcon">
  64.                     <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>
  65.                 </div>
  66.     </div> 
  67. </div>
  68.  
  69.   <!--End RelatedTopics Section-->
  70.  
  71.     </div>
  72.  
  73. </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]

  1.    ({
  2. 	doInit:function(component,event,helper){  
  3.        var action = component.get("c.custoLabelItem"); 
  4.  
  5.         action.setCallback(this,function(response){  
  6.             var state = response.getState();  
  7.             if(state=='SUCCESS'){  
  8.                 var result = response.getReturnValue();           
  9.                 component.set("v.customLablVal",result);  
  10.             }  
  11.         });  
  12.         $A.enqueueAction(action);
  13.     },
  14.  
  15.     customLabelShow : function(component, event, helper) {        
  16.         component.set("v.custmLblStg",true); 
  17.     },
  18.  
  19.    customLabelHide : function(component, event, helper) {
  20.         component.set("v.custmLblStg",false); 
  21.     },
  22.  
  23. })

 

Create Apex Class Controller →

Step 3:- Create Apex Class : dynamicCustomLabelCtrl.apxc

From Developer Console >> File >> New >> Apex Class

dynamicCustomLabelCtrl.apxc [Apex Class Controller]

  1.     public class dynamicCustomLabelCtrl {
  2.     //how TO find a specific custom label IN apex IN lightning component
  3.     @AuraEnabled 
  4.     public static String custoLabelItem(){
  5.         List<String> custmValParm = NEW List<String>();
  6.         String custmLbl = Label.w3web_Salesforce_Tutorial;
  7.         String custmLblMsg = String.format(custmLbl, custmValParm);
  8.         System.debug('CustomLabelValue' + custmLblMsg);
  9.         RETURN custmLblMsg;
  10.     }  
  11. }

 

Create Lightning Application →

Step 4:- Create Lightning Application : dynamicCustomLabelCmpApp.app

From Developer Console >> File >> New >> Lightning Application

dynamicCustomLabelCmpApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:dynamicCustomLabelCmp/>
  3. </aura:application>

 
get specific value of custom label in apex -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment