Apex Controller:
public with sharing class RefreshApexController {
@AuraEnabled(cacheable=true)
public static String getLabels(){
System.debug('I am inside getLabels() method');
return System.Label.Number_of_Shifts;
}}
HTML code:
<template>
<lightning-card title="Custom Label Example" variant="narrow" icon-name="standard:opportunity">
<p>{label.WelcomeLabel}</p>
</lightning-card>
<lightning-card title="Custom Label Example" variant="narrow" icon-name="standard:opportunity">
<p>{record}</p>
</lightning-card>
</template>
JS:
import { LightningElement, wire, track } from 'lwc';
import WelcomeLabel from '@salesforce/label/c.Number_of_Shifts';
import getLabels from '@salesforce/apex/RefreshApexController.getLabels'
export default class CustomLabel extends LightningElement {
@track record;
@track error;
label = {
WelcomeLabel,
};
@wire(getLabels)
wiredAccount({ error, data }) {
if (data) {
this.record = data;
this.error = undefined;
console.log(this.record);
} else if (error) {
this.error = error;
this.record = undefined;
}
}
}
Problem Statement: I am trying to get the Labels from salesforce using the conventional method and by utilizing wire function. The main aim here is to get the new value (without any delay) when the Label value changes in salesforce.
If I go with the traditional way by importing Labels modules from salesforce the value is not changing instantaneously even after refreshing the page. Whereas in the case of wire function the value is changing but the getLabels() method is being called every time after the page is refreshed.
Ask:
- Is the above code working as designed?
- What is the best practice to get the new Label value as soon as it changes in salesforce?
- Is there any way I can use refreshApex() in this context?