1

RefreshApex works for me so far in almost every case. I'd like to understand why I would instead use notifyRecordUpdateAvailable or the RefreshView API instead. Or am I horribly misunderstanding the purpose of these tools?

Based on my reading of the documentation, notifyRecordUpdateAvailable is really only needed when the two following conditions are met:

  • You've updated a record outside of the mechanisms of LDS, such as imperative Apex
  • The provisioned records from the wire service are from non-custom Apex, such as from the getRecord or getRecords methods from the 'lightning/uiRecordApi' module

I've created a demo for notifyRecordUpdateAvailable but it doesn't seem to matter whether I use that method or refreshApex, the effect is the same, with the cache updating and all components getting the new data.

notifyRecordUpdateAvailableDemo.js:

import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import { getRecord, notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import RATING_FIELD from '@salesforce/schema/Account.Rating';
import setAccountRatingToHot from '@salesforce/apex/AccountHelper.setAccountRatingToHot';

export default class NotifyRecordUpdateAvailableDemo extends LightningElement {

    @api
    recordId;

    @wire(getRecord, { recordId: '$recordId', fields: RATING_FIELD })
    accRecord;

    async handleRatingChange() {
        await setAccountRatingToHot({ AccId: this.recordId });
        //await notifyRecordUpdateAvailable([{recordId: this.recordId}]);
        refreshApex(this.accRecord);
    }
}

notifyRecordUpdateAvailableDemo.html:

<template>
    <lightning-button variant="brand" onclick={handleRatingChange} label="Set Account Rating to Hot">
    </lightning-button>
</template>

AccountHelper.cls

public with sharing class AccountHelper {
    public AccountHelper() {

    }

   @AuraEnabled
    public static void setAccountRatingToHot(String AccId){
        try {
            Account acc = [SELECT Id, Rating FROM Account WHERE Id = :AccId];
            acc.Rating = 'Hot';
            update acc;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}

1 Answer 1

0

Knowledge on RefreshView API is still lacking. But I (think) can at least answer the question of RefreshApex vs notifyRecordUpdateAvailable.

RefreshApex is great for most situations. It will provision new data for wire adapters and will also update standard Salesforce UI elements with fresh data.

notifyRecordUpdateAvailable is used when you need to inform other components or standard Salesforce UI elements that a record was updated outside of the mechanisms of the Lightning Data Service (i.e. our 'updating' component isn't using the wire service at all). Here is an example that is more clear than what is found in the documentation (in my opinion):

This component is intended to be placed on Account record pages. When the user clicks the button, the Account's 'Rating' field is changed to 'Hot'.

<template>
    <lightning-button variant="brand" onclick={handleRatingChange} label="Set Account Rating to Hot">
    </lightning-button>
</template>

Here the notifyRecordUpdateAvailable method will inform other, separate, components that use the wire service, or standard Salesforce UI such as record detail pages, that the record has been updated and the cache is stale. This will refresh the data for those components.

import { LightningElement, api } from 'lwc';
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import setAccountRatingToHot from '@salesforce/apex/AccountHelper.setAccountRatingToHot';

export default class NotifyRecordUpdateAvailableDemo extends LightningElement {

    @api
    recordId;


    async handleRatingChange() {
        await setAccountRatingToHot({ AccId: this.recordId });
        await notifyRecordUpdateAvailable([{recordId: this.recordId}]);
    }
}
public with sharing class AccountHelper {
   
    @AuraEnabled
    public static void setAccountRatingToHot(String AccId){
        try {
            Account acc = [SELECT Id, Rating FROM Account WHERE Id = :AccId];
            acc.Rating = 'Hot';
            update acc;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}

You must log in to answer this question.

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