Lightning

AURA: Clear cache while loading a Lightning Component.

Usually because of cachable=true in apex class, you see results in lightning Aura component same as previous one, Event thought the records are updates, you do not see any updated values. To prevent Lightning aura component from loading component with same results we need to clear cached value.

To clear cache, you have to fire refreshview on component as shown below.


Aura Component’s html file.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,force:lightningQuickAction,force:hasSObjectName,lightning:isUrlAddressable" 
                access="global" controller="Sample_Controller">
				
		<aura:handler name="change" value="{!v.pageReference}" action="{!c.onPageReferenceChanged}" />
		
		
		<!-- Aura component form -->
		
</aura:component>

Aura Component’s Controller.JS file

({
	onPageReferenceChanged: function(cmp, event, helper) {
        $A.get('e.force:refreshView').fire();
    }
})

Note:

  • The force:refreshView event impacts performance and should be avoided, if possible.
  • This event is supported in Lightning Experience, the Salesforce mobile app, and Aura-based Experience Builder sites.

Reason behind this post.

I was working on a custom component and there were many related lists for a record. When clicking on a new button I had to load a custom create form with preloaded lookup field.

It was not possible to fetch the record that I recently viewed, in custom component. Although i queried RecentlyViewed sObject, Some times it used to load some old values and I had to wait for some time (Delay in apex) before querying the same.

Instead you can use a refreshView to clear cache, and run the apex again you can get Recently Viewed record and use it as a lookup or part of related list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s