If you are looking for a solution to refresh your custom Lightning Web Component on change of data in one of the Standard Component or from Backend. This blog is for you!
To refresh the APEX in LWC we have refreshApex(), to refresh the standard components on change of custom LWC we have refreshViewAPIs
To refresh a Custom LWC whenever data is changing on Standard component or in backend unfortunately there are no OOTB way to achieve it. But we are here to create a workaround of it.
As a workaround we will use Platform Events to notify custom LWC that something has changed in data and it needs to refresh.
Please check steps below:
Step 1: Create a Platform Event as shown below.
Step 2: You need to publish the platform event using an automation flow or trigger. In my scenario I have used triggers.
I have created OpportunityTrigger and published it on AfterUpdate event. As a best practice you can modify the code accordingly and publish the platform event on change of specific fields only!
public without sharing class OpportunityTriggerHandler {
private boolean triggerIsExecuting=false;
private integer triggerSize=0;
public OpportunityTriggerHelper helper;
public OpportunityTriggerHandler(boolean triggerIsExecuting, integer triggerSize) {
this.triggerIsExecuting = triggerIsExecuting;
this.triggerSize = triggerSize;
this.helper = new OpportunityTriggerHelper();
}
public void beforeInsert(List<Opportunity> newOpportunities) {
// helper.doTask1();
// helper.doTask2();
}
public void beforeUpdate(List<Opportunity> oldOpportunities, List<Opportunity> newOpportunities, Map<ID, SObject> oldOpportunitiesMap, Map<ID, SObject> newOpportunitiesMap) {
// helper.doTask3();
// helper.doTask4();
}
public void beforeDelete(List<Opportunity> oldOpportunities, Map<ID, SObject> oldOpportunityMap) {
// helper.doTask5();
// helper.doTask1();
}
public void afterInsert(List<Opportunity> newOpportunities, Map<ID, SObject> newOpportunityMap) {
// helper.doTask2();
// helper.doTask3();
}
public void afterUpdate(List<Opportunity> oldOpportunities, List<Opportunity> newOpportunities, Map<ID, SObject> oldOpportunityMap, Map<ID, SObject> newOpportunityMap) {
helper.componentRefreshPE();
}
public void afterDelete(List<Opportunity> oldOpportunities, Map<ID, SObject> oldOpportunityMap) {
// helper.doTask3();
// helper.doTask1();
}
public void afterUndelete(List<Opportunity> newOpportunities, Map<ID, SObject> newOpportunityMap) {
// helper.doTask4();
// helper.doTask2();
}
}
OpportunityTriggerHelper.cls
1
2
3
4
5
6
7
8
9
10
public with sharing class OpportunityTriggerHelper {
public OpportunityTriggerHelper() {
System.debug('Inside OpportunityTriggerHelper Constructor');
}
public void componentRefreshPE() {
System.debug('componentRefreshPE');
Refresh_Custom_Components__e refreshEvent = new Refresh_Custom_Components__e();
EventBus.publish(refreshEvent);
}
}
Step 3: Now you can subscribe to this Platform Event from your custom LWC and using refreshApex you can refresh the component whenever the event is bubbling up as shown below!
OpportunityOverAmountApex.cls
1
2
3
4
5
6
public with sharing class OpptiesOverAmountApex {
@AuraEnabled(cacheable=true)
public static List<Opportunity> getOpptyOverAmount(Id recordId) {
return [SELECT Id, Name, Amount, StageName, CloseDate FROM Opportunity WHERE AccountId=:recordId];
}
}
0 Comments