0

I'm working with simple LWC component in community which is basically a form where user can update information. During page load, wire service fetches data from apex and once user submit the form, component calls a imperative method to update it. If promise is successful, refreshapex module should fetch the latest data. However it's not fetching the latest data that I've verfied in console as well. Please suggest where I'm wrong. I've seen multiple post but none of them worked for me.

JS

import { LightningElement, api, wire, track } from "lwc";
import getHOdetails from "@salesforce/apex/CreateUserUnderHO.getHOdetails";
import updateHOuser from "@salesforce/apex/CreateUserUnderHO.updateHOuser";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { getPicklistValues } from "lightning/uiObjectInfoApi";
import { refreshApex } from "@salesforce/apex";
import CONTACT_SALUTATION from "@salesforce/schema/Contact.Salutation";
import CONTACT_OBJECT from "@salesforce/schema/Contact";

export default class Ei_changeMyDetailsHO extends LightningElement {
  @track contactRecord = CONTACT_OBJECT;
  @track updateRecord = CONTACT_OBJECT;
  @track isEditModeOn = false;
  @track isValueUpdated = false;
  @track error;

  @track marketingAgreementChecked;
  @track newsletterSubscriptionChecked;

  // Wire Decorater to get Picklist Value of Salutation from Contact Object and strore in salutationOptions track property
  @wire(getPicklistValues, {
    recordTypeId: "012000000000000AAA",
    fieldApiName: CONTACT_SALUTATION
  })
  wiredPickListValue({ data, error }) {
    if (data) {
      this.salutationOptions = data.values;
      this.error = undefined;
    }
    if (error) {
      this.error = error;
      this.salutationOptions = undefined;
      this.showToastMessages("Error !!", error, "error", "dismissable ");
    }
  }

  @wire(getHOdetails)
  wiredAccounts({ error, data }) {
    if (data) {
      this.updateRecord.Id = data.Contact.Id;
      this.contactRecord = data.Contact;
      this.marketingAgreementChecked = this.contactRecord.Marketing_Agreement__c;
      this.newsletterSubscriptionChecked = this.contactRecord.Newsletter_Subscription__c;
    } else if (error) {
      this.error = error;
      this.showToastMessages("Error !!", error, "error", "dismissable ");
    }
  }

Once data is saved imperative method is called :

    updateHOuser({
  con: this.updateRecord
})
  .then((result) => {
    this.showToastMessages(
      "Success !!",
      "Information has been updated",
      "success",
      "dismissable "
    );

    delete this.updateRecord.objectApiName;
    this.contactRecord = {};

    console.log(`${JSON.stringify(this.contactRecord)}`);;
    this.isEditModeOn = false;

    refreshApex(this.contactRecord);
    return refreshApex(this.contactRecord);
  })
  .catch((error) => {
    console.log("error:", error);
    this.error = error.message;
    this.showToastMessages("Oops !!", error.message, "error", "dismissable ");
  });

Cacheable=true for wired method

1 Answer 1

2

You need to keep the original wire data in order to work:

wiredAccountsResult;
wiredAccounts(wiredResult) {
let { error, data } = wiredResult;
this.wiredAccountsResult = wiredResult;
...

And then:

refreshApex(this.wiredAccountsResult);

Also, you don't need to call it twice:

  // This refresh wasn't necessary //
  // refreshApex(this.wiredAccountsResult);
  return refeshApex(this.wiredAccountsResult);
}
2
  • Thank you so much for the explanation. This means while working with wire service, the original data received from apex, should be stored in a property and whenever required this property should be used to refresh apex.
    – wink
    Commented Dec 21, 2020 at 6:29
  • I've a cancel button on my html which should undo all change made on UI input. I'm calling handleCancel method method as shown below: handleCancel(event) { this.isDisabled = true; this.isValueUpdated = false; refreshApex(this.wiredContactResult); }
    – wink
    Commented Dec 21, 2020 at 11:01

You must log in to answer this question.

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