0

I have an apex class that passes in the value of 'lastCallSource' to do a SOQL query on a Custom MetaData Type that will display a custom field value from the metaData to the user. I have this working, however the error handling does not populate the error to the LWC without refresh. Code and explanations below:

@AuraEnabled(cacheable=true)
public static String getConfirmSource(String lastCallSource){
System.Debug('LastCallSource = ' + lastCallSource); 
    String company;
    String metaRecord;  
    CustomMeta__mdt myData = new CustomMeta__mdt();
    try { myData = [SELECT Id, Company__c FROM CustomMeta__mdt  WHERE TrackingSource__c =: lastCallSource LIMIT 1]; 
    company = myData.AutoCompany__c;
    metaRecord = myData.Id;  
        System.debug('Leaving the TRY STATMENT'); 
    } catch (Exception e) {
        System.debug('Error creating metadata: ' + e.getMessage());

    } 

This APEX method is called in the lwc component:

import { refreshApex } from '@salesforce/apex';
import getConfirmSource from '@salesforce/apex/AgentStateTracker.getConfirmSource';

export default class DisplayCompany extends LightningElement {

    @track company;
    @track error; 

    @api
    lastCallSource;


    @wire(getConfirmSource, {lastCallSource: '$lastCallSource'} )
    wiredMetaData({error,data}){
       if(data){ 
        this.company = data;
        this.error = undefined;


       } else if(error) {
           this.error = error; 
           this.company = 'Unknown'; 


       }
2

As you said that initially data is correctly shown to user but It is not clear of why you should be getting error.

However, the refresh issue might be because of cacheable=true. There are 2 options.

  1. Use wire method as above and use refreshApex() to refresh cache manually. Below can be done whenever needed in a method:

    return refreshApex(this.company);
    
  2. Remove cacheable=true and use imperative call (as you cant use wire with non-cacheable method) like below:

.

connectedCallback(){
    this.fetchData();
}

fetchData(){
    getConfirmSource({ lastCallSource: this.lastCallSource })
      .then(response => --logic--)
      .catch(err => console.error(err));
}
  • Testing it now. Do you know if you would need to call refreshApex() afterwards? or should the connectedCallBack() method take care of it? – thinker Jul 30 at 14:07
  • 1
    refreshApex is applicable to wired functions or properties – salesforce-sas Jul 30 at 14:26
  • Thanks for your help! IT WORKED! If possible, I would give you 100 upvotes! Thanks again. – thinker Jul 30 at 15:07
  • WC :) . Pls select solved if solved. – salesforce-sas Jul 30 at 15:07
  • I marked it as accepted. Is that equivalent? – thinker Jul 30 at 18:22

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.