0

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:

  1. Is the above code working as designed?
  2. What is the best practice to get the new Label value as soon as it changes in salesforce?
  3. Is there any way I can use refreshApex() in this context?
4

Is the above code working as designed?

Yes.

What is the best practice to get the new Label value as soon as it changes in salesforce?

In practice, labels rarely change, if ever. You should use the import label statement for performance reasons.

Is there any way I can use refreshApex() in this context?

You can call that on the method, but that's overkill. There's simply no reason to do this. If your labels are changing "every five minutes", then you're probably not using labels the way they are intended to be used.

The exception to this is during development. While developing, make sure that you disable the Lightning Component Cache. This gives you the immediate updates you're looking for, but will cost additional loading times during refreshes.

2
  • Thanks for the quick response. Would you mind explaining how the above code is working as designed as cacheable=true is not considered and every time a server call is happening. I have enabled the caching option in Session Settings. So Ideally caching should work and there shouldn't be a server call.
    – charan
    Aug 18 '20 at 5:33
  • Got it, because of the browser refresh I am seeing the server call.
    – charan
    Aug 18 '20 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.