Skip to main content

Hello,

 

I have created one LWC component where I'm using the registerRefreshHandler from 'lightning/refresh'  to refresh my component automatically (instead of refreshing the whole page manually) which is called in connected callback.

 

But, the issue is sometimes, when I refresh the page manually, the page keeps on refreshing in recursive loop.

 

Below is my sample code:

import { LightningElement, api, track } from 'lwc';

import getRelatedCaseAndOrderStatus from '@salesforce/apex/caseOrderStatus.getRelatedCaseAndOrderStatus';

import getRewardAppCustomers from '@salesforce/apex/caseOrderStatus.getRewardAppCustomers';

import getCaseAndOrderCustomerId from '@salesforce/apex/caseOrderStatus.getCaseAndOrderCustomerId';

import getCustomerOrderHistory from '@salesforce/apex/caseOrderStatus.getCustomerOrderHistory';

import {registerRefreshHandler, unregisterRefreshHandler} from 'lightning/refresh';

import SVGICONS from '@salesforce/resourceUrl/SVGIcons';

export default class CustomerSegments extends LightningElement {

firstTimerOutputValue = false

explorerOutputValue = false

atRiskRetainerOutputValue = false

loyalOutputValue = false

churningOutputValue = false;

AtRiskoutputValue= false;

CriticaloutputValue= false;

rewardsAppOutputVal= false;

@api recordId;

@api objectApiName;

refreshHandlerID;

firstTimerUrl = SVGICONS+'/Icons_Firsttimer.svg';

explorerUrl = SVGICONS+'/Icons_Explorer.svg';

atRiskRetainerUrl = SVGICONS+'/Icons_AtRiskRetainer.svg';

loyalUrl = SVGICONS+'/Icons_Loyalcustomer.svg';

churningUrl = SVGICONS+'/Icons_Churning.svg';

atRiskUrl = SVGICONS+'/Icons_Atriskcustomer.svg';

criticalUrl = SVGICONS+'Icons_Criticalcustome.svg';

rewardsUrl = SVGICONS+'/Icons_RewardsAppCustomer.svg';

@api id = 1;

@api label = 'Customer Segment';

customerId;

brandId;

lastOrderedDate;

countOfOrders;

lastSecondOrderedDate;

toggleSection(event) {

let buttonid = event.currentTarget.dataset.buttonid;

let currentsection = this.template.querySelector('[data-id="' + buttonid + '"]');

if (currentsection.className.search('slds-is-open') == -1) {

currentsection.className = 'slds-section slds-is-open';

} else {

currentsection.className = 'slds-section slds-is-close';

}

}

connectedCallback(){

this.refreshHandlerID = registerRefreshHandler(this, this.refreshHandler);

this.loadInitialData();

}

refreshHandler(){

return new Promise((resolve) =>{

this.loadInitialData();

resolve(true);

})

}

loadInitialData(){

getRelatedCaseAndOrderStatus({ "recordId" : this.recordId})

.then(result => {

})

.catch(error => {

console.log(error);

})

getRewardAppCustomers({ recordId : this.recordId})

.then(result => {

})

.catch(error => {

console.log(error);

})

getCaseAndOrderCustomerId({recordId : this.recordId})

.then(result => {

this.customerId = result.customerId;

this.brandId = result.brandId;

console.log('Assignmemt Successful, calling API class')

if(this.customerId !== '' && this.brandId !== ''){

getCustomerOrderHistory({customerId : this.customerId, brandId : this.brandId})

.then((result) => {

})

.catch(error => {

console.log(error);

})

}

})

.catch(error => {

console.log(error);

})

}

disconnectedCallback() {

unregisterRefreshHandler(this.refreshHandlerID);

}

}

Can someone please help me why it is happening?

 

Also, one more point, the below setting was not enabled in my org for previous LWC component. I had to enable this because registerRefreshHandler does not work without enabling that setting and the component throws an error. 

Session Setting --> Use Lightning Web Security for Lightning web components and Aura components

 

#Salesforce Developer

3 answers
0/9000