1

On the Account record page, I want to refresh my LWC page whenever I create or edit a related contact record. My LWC page is fetching list records from apex not using LDS.

I couldn't think of any way to achieve the same so I thought of wrapping my LWC in the aura and use the below aura code.

<aura:component implements="flexipage:availableForRecordHome">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:dependency resource="markup://force:editRecord" type="EVENT" />

    <aura:handler event="force:refreshView" action="{!c.doInit}" />
</aura:component>

JS:

doInit: function (component, event, helper) {
    console.log('from aura');
},

I get a console log when I edit the account record but don't get anything when I edit the contact record from the related list of account.

is there any way I can detect if a record is created or updated from the related list either in lwc or aura?

2
  • Yes, you can use the Change Data Capture and Streaming APIs. Commented Jul 26, 2021 at 16:14
  • Yes I think of streaming API too, but don’t want to use them as they comes with daily limit of 50k or 10k
    – gs650x
    Commented Jul 26, 2021 at 16:17

1 Answer 1

0

You should be able to run your imperative Apex call from a getRecord adapter:

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import myMethod from '@salesforce/apex/MyClass.myMethod';

export default class MyLWC extends LightningElement {
    @api recordId;
    result;
    
    @wire(getRecord, { recordId: '$recordId', fields: ['Account.Id'] })
    getRecordData({ error, data }) {
        if (this.recordId) {
            refreshApex(this.result);
            this.imperativeApexFunction();
        }
    }
}

imperativeApexFunction() {
    myMethod({ recordId: this.recordId })
        .then( result => {
            console.log(result);
            this.result = result;
            // Handle result
        })
        .catch( error => {
            console.log(error);
            // Handle error
        })
}

The @wire getRecord adapter is intrinsically connected to the LDS, and will automatically run when a change is saved to the parent record, or a related child record, causing your Apex method to rerun as well

The recordId is not always set by default on page load, and it is helpful to include a check to see if there is a recordId before running your Apex method to prevent null errors

7
  • Thank you for your answer, unfortunately, it didn't work.
    – gs650x
    Commented Jul 26, 2021 at 17:55
  • for updating account it works, but for updating or creating contact it doesnt
    – gs650x
    Commented Jul 26, 2021 at 18:01
  • I've made an edit to include a refreshApex call - does that make any difference?
    – Instread
    Commented Jul 26, 2021 at 18:02
  • This didn't work either.
    – gs650x
    Commented Jul 26, 2021 at 18:05
  • Is your LWC a component on the Account Record Page? Or is it on a separate page? I just tested this as a component on the Account page and it worked in my dev org
    – Instread
    Commented Jul 26, 2021 at 18:46

You must log in to answer this question.

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