0
@wire(fetchCustomSettings)
wiredData;

renderedCallback() {
    if (this.wiredData) {
        console.log('this.wiredData ##', this.wiredData.data);
        refreshApex(this.wiredData);
        console.log('this.wiredData ##2222', this.wiredData.data);

    }
}

Apex :

 @AuraEnabled(cacheable=true)
 public static Boolean isSiteUnderMaintenance() {
        SiteMaintenance__c siteMaintenanceSettings = SiteMaintenance__c.getInstance();
    if (siteMaintenanceSettings != null && siteMaintenanceSettings.IsUnderMaintenance__c != null) {
        return siteMaintenanceSettings.IsUnderMaintenance__c;
    } else {
        return false;
    }
}

My need is to get the returned value from Apex and refresh it every time i change the value the custom setting field to True/False then if true do some logic but on page init the console shows undefined why the wire doesn't retrieve data?

Besides , when i change the value of the field in salesforce to true/false , it doesn't get refreshed in the lwc

PS : My lwc html is empty , my goal is the get the value of custom settings field and do some logic if it's true but i noticed that its not refreshed ..

5
  • 1
    You are making this far more complex than it need be. If you do not want the settings data to be cached, call the Apex imperatively. And remove cacheable=true, of course.
    – Phil W
    Commented Sep 4, 2023 at 19:13
  • Also, to have an empty HTML actually use <template></template>.
    – Phil W
    Commented Sep 4, 2023 at 19:14
  • No Phil , i don't want to call the Apex Imperatively since i want html empty I removed caceable=true and i used wire as a function but no console shows @wire(fetchCustomSettings) wiredCustomSettings({ error, data }) { if (data) { this.testo = data; this.isUnderMaintenance = data; // Set isUnderMaintenance with the data from the wire } else if (error) { console.error('Error fetching custom settings:', error); } }
    – Fark
    Commented Sep 4, 2023 at 19:19
  • Imperative apex doesn't mean you cannot have an empty template. Also wires require cacheable=true.
    – Phil W
    Commented Sep 4, 2023 at 19:20
  • You right . Ma man
    – Fark
    Commented Sep 4, 2023 at 19:30

2 Answers 2

0

My lwc html is empty

Maybe because by having nothing to render, it doesn't render.

Did you try using the connectedCallback handle instead? This one is sure to run when the component is added to the page.

2
  • Ofc but with connectedCallback , on page load 1st time it shows undefined , then when i navigate to other page and return to the concerned page it shows the right value (True/False) but when i change the value in salesforce => in Lwc it doesn't get refreshed ...
    – Fark
    Commented Sep 4, 2023 at 19:02
  • In this case I think you might not want to cache this property. Caching is more useful for record data, not custom settings that might change suddenly. If you call your Apex method imperatively, it will return the value of the custom setting, not the cached value. This might work better in your scenario. Commented Sep 4, 2023 at 19:06
0

Your problem stems from the fact that there is a particular order of operations of @wire in LWC. connectedCallback and renderedCallback will be called once before wire has a chance to retrieve data from the server. In addition, render and renderedCallback will only trigger initially, and then after a change is detected in a property, either by @track or because a top-level property bound in the template changes.

You need to bind wiredData directly if you use a wired property, such as lwc:if={wiredData.data}, or you need to set a property that is bound to the template, such as:

<template lwc:if={isSettingEnabled}>...</template>
isSettingEnabled = false;
@wire(fetchCustomSettings) wiredData({ data, error }) {
  if(data !== undefined) {
    this.isSettingEnabled = data;
  }
  if(error) {
    this.dealWithError(error);
  }
}

Either way, changing either of these conditions will work. Note that if you're using @wire, and you periodically change the custom settings, you need to use refreshApex. Otherwise, wire might not be the method for you. Instead, consider checking the setting when it is relevant:

async someEventHandler(event) {
  const settingEnabled = await fetchCustomSettings();
  if(settingEnabled) {
    this.doSomething();
  } else {
    this.tellUserFeatureDisabled();
  }
}

You can also do this same technique on connectedCallback to check the initial status. You could set up a polling loop every few minutes to detect if the setting has changed, and if so, respond accordingly.

It's not practical to have the server being repeatedly polled for a status flag, so I'd personally recommend a check on connectedCallback and another check before doing an action that involves the server, as well as perhaps an infrequent check every few minutes if you want to advise about maintenance mode automatically.

2
  • Thanks for your answer , i resolved it but call the Apex imperatively in connectedCallBack , Your point of polling loop every few minutes is interesting , do you have an example please ?
    – Fark
    Commented Sep 5, 2023 at 8:40
  • @Fark It's as easy as timerId; connectedCallback() { this.timerId = setInterval(() => this.checkStatus(), 60000); } disconnectedCallback() { clearInterval(this.timerId); }, where 60000 would check every minute.
    – sfdcfox
    Commented Sep 5, 2023 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .