function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
charbel ALALAMcharbel ALALAM 

What would you like to know?

hi guys, 
i have a question plz:
How can i get a reference to a custom label dynamically using lwc:
i have this code and i want to convert it to lwc:
var currentCat = categoryWrapper.categories[i];  var labelReference = $A.getReference("$Label.c."+currentCat.name);   component.set("v.tempLabel", labelReference);
//we can't use $A.getreference in lwc,
 and using apex i didn't found a solution 
this is the function in apex:
 @AuraEnabled     public static String getLabel(String LabelName){                  system.debug('LabelName'+LabelName);         String s2 = system.Label.LabelName ;  return s2;     }
this is the function in lwc:
 getLabel({
                    LabelName:labelReference, //labelreference is the name of custom label
                }).then(result=>{
                    
                    console.log('myJSON:'+result);
 
                }).catch(error=>{
                    console.log('error');
                })
//i want to return the value of a custom label when i send the name of custom label in parameter. Where Is my error, and is there a solution ?
Best Regards
Surya GSurya G
Hi Charbel,
Have you imported the controller class in LWC?
here is sample code example to call your apex class with dynamic custom label name.
import { LightningElement, wire } from 'lwc';
import getLabel from '@salesforce/apex/controllerName.getLabel';

export default class customLabelLWC extends LightningElement {
    labelValue;
    
    get labelReference() {
        // write  your logic to get custom label name dynamically
    }

    @wire(getLabel, { LabelName: '$labelReference' })
    wiredLabel({ error, data }) {
        if (data) {
            this.labelValue = data;
        } else if (error) {
            console.log('error='+error);
        }
    }
}

whenever your custom label name changes, lwc will automatically call apex method 'getLabel' and return the respective custom label value.
let me know if it helps

Thanks
Surya G




 
charbel ALALAMcharbel ALALAM
hi suya, 
yes i imported the controller, the problem is how to get the custom label dynamically in apex.
this is the function in apex:
@AuraEnabled
    public static String getLabel(String LabelName){
        //Labelname is the name of the custom labem
        system.debug('LabelName'+LabelName);
        String s2 = system.Label.LabelName ;// In s2 i want to return the value of the custom label
        return s2;
    }
the problem in apex is (External string does not exist: LabelName).
If i use the name of the LabelName directly like this:
String s2 = system.Label.B2B_CUL_Textile;//It return the value of custom label which is Textile
//the get function receive the name of custom label and i want it to return the value of this custom label
Surya GSurya G
Hi charbel,

Yes. you are correct. we cannot use apex to dynamically call custom label unless it is called as visualforce controller
And unfortunately we cannot import custom label dynamically in lwc.

But, as a workaround, we can have utility service component in LWC that will import all the custom label in your org, and we can extend that class in your main component that will get the custom label value dynamically.

service component:
import { LightningElement } from 'lwc';
import Test_1 from '@salesforce/label/c.Test_1';
import Test_2 from '@salesforce/label/c.Test_2';
// import all label in this component

export default class CustomLabelService extends LightningElement {

   // add all the custom label name in this object
   customLabels = {
        getTest_1 : Test_1,
        getTest_2 : Test_2
    }

    // method called from your main class(lwc)
    getCustomLabelValue(labelName) {
        return this.customLabels['get'+labelName];
    }
}
main component:
import { LightningElement, wire } from 'lwc';
import CustomLabelService from 'c/customLabelService';

export default class customLabelLWC extends CustomLabelService {
    labelValue;
    labelName = 'Test_2'; // change the custom label name as required

    connectedCallback(){
        this.labelValue = this.getCustomLabelValue(this.labelName);
    }
}
This become more complex if your org has many custom labels a you have to import everything in service component, also we have to add label to service component if any new custom label is created.

Thanks
Surya G