0

I am creating Dependent combo box using refresh apex. The second apex method is being called but in the UI i get an error message as below

I implemented the solution as mentioned in here Can't get refreshApex() to work in lwc but still i get the error. so i wanted a solution.

Here is my code.

import { LightningElement, wire, track } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getDivisions from 
'@salesforce/apex/TrafficReportController.getDivisions';
 import getCommunities from 
'@salesforce/apex/TrafficReportController.getCommunities';
 var commdata;
 var commerror;
 export default class TrafficHistoryReport extends LightningElement {   
 @track divisionvalue;   
 @track communityvalue;
 @track Divisionvalues = [];
 @track Communityvalues = [];
 @track wiredCommunities =[];
 @wire(getDivisions) 
 ListDivisions({data,error}){
 if(data){ 
    for(let i=0;i < data.length;i++){
        this.Divisionvalues.push({
            label: data[i].Name,
            value: data[i].Id
        });
    }
    //this.divisionoption = this.Divisionvalues;
}
else if (error){
    window.console.log(error);
}
}

 @wire(getCommunities, {divId: '$divisionvalue'}) 
   getwiredCommunities(result){
    this.wiredCommunities = result;   
    commdata = result.data;
    commerror = result.error;          
    if(commdata){                 
        this.Communityvalues = commdata.map(record => ({label: record.Name, value: record.id}));                          
    }
    else if (commerror){
        window.console.log(commerror);
    }
}


 get divisionoption() {
//console.log(this.Divisionvalues);
return this.Divisionvalues;
}

get communityoption() {
//console.log(this.Divisionvalues);
return this.Communityvalues;
}

onDivisionSelect(event){
//this.divisionvalue = event.target.value;
return refreshApex(this.wiredCommunities);
//event.preventDefault();
//console.log('value'+event.target.value)
}

handlechange(event){
event.preventDefault();
}

}
  • 2
    What error are you getting? On which line? Please edit to clarify. – David Reed Dec 9 '19 at 17:06
  • Don't try to track the property/ies used to hold the wire results; this isn't necessary since you will only be using these for the refreshApex invocations. The rest of the time you want to track the data extracted from the "data" property of the wire result... – Phil W Dec 9 '19 at 17:27
0

Note that you have a dynamic binding for passing a parameter to the wire. You should probably store the wire response in a "map" based on this parameter so you can clear the correct cache result:

wiredCommunities = {};

divisionValue = null;

onDivisionSelect(event) {
    this.divisionValue = event.target.value;

    if (this.wiredCommunities[this.divisionValue]) {
        refreshApex(this.wiredCommunities[this.divisionValue]);
    }
}

@wire(getCommunities, {divId: '$divisionvalue'}) 
getwiredCommunities(result) {
    this.wiredCommunities[this.divisionValue] = result;   
    commdata = result.data;
    commerror = result.error;          

    if (commdata) {                 
        this.Communityvalues = commdata.map(record => ({label: record.Name, value: record.id}));                          
    } else if (commerror) {
        window.console.log(commerror);
    }
}

This way you remember the wire call for a specific divisionValue and cause the cache for it to be cleared for that specific invocation. Remember that the set of parameter values to a wire select the cache entry used.

| improve this answer | |

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.