[SalesForce] Update custom Lightning Component on Master when Detail “Related List” Component edited

I have a custom Lightning Component on an Opportunity Record FlexiPage, that shows a Master-Detail relationship Rollup Calculation (e.g. the count of detail records). After adding an item via the "Related List" standard Lightning Component, the rollup value displayed in my custom component is incorrect; I would like for it to be updated. See image below:

Depiction of the current problematic layout

I suspect it may not currently be possible to accomplish easily.

I had hoped that the Lightning Data Service, which is aware of edits to the same object by other standard Lightning Components on the page, would also recognize related edits by standard Lightning Components (e.g. Related List Component). Unfortunately, even the standard Detail Panel Lightning Component does not get refreshed from Related List inserts, until I manually touch it in some other way. So it's probably not just some incomplete implementation on my part.

Alternately, I had hoped I could get a notification when the Save button was clicked on the Related List component, but the force:refreshView event in our component isn't isn't raised upon changes to custom sObjects in Related Lists. Maybe I can still derive from the standard Related List, capture the Save event there, drop that custom implementation on the page, and then use a page-wide event to get notification over. Unfortunately, that creates a weakness in my custom component, where it breaks if my organization rebuilds the page naively.

As a last resort, I suppose I could leverage the Streaming API to capture related entity changes directly at the back end. Unfortunately, I've seen that this is still poorly supported for Lightning components.

Best Answer

Update: After detailed discussion with OP, we realized that standard related list component is not firing refreshView event if it is for custom object. More details in this question : Standard "Related List - Single" component fires refreshView event only in certain cases

Ok.The below solution is kind of a hack on the assumption that standard related list component shows toast after new child record is added or existing child record is deleted or existing child record is edited.

I created a simple custom component to show Opportunity amount and there is a standard related list component showing Opportunity products on the same page.

Here is the code:

OpportunityAmountComponent.cmp:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" controller="OpportunityAmountComponentController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="oppAmount" type="String" default="0.00"/>
    <aura:handler event="force:showToast" action="{!c.doInit}"/>
    <ui:outputText value="{!v.oppAmount}" />

</aura:component>

OpportunityAmountComponentController.cls:

public class OpportunityAmountComponentController {

    @AuraEnabled
    public static String getOpportunityAmount(String oppId){
        return String.valueOf([SELECT Amount FROM Opportunity WHERE Id=:oppId].Amount);
    }

}

OpportunityAmountComponentController.js:

({
    doInit : function(component, event, helper) {
        debugger;
        var action = component.get("c.getOpportunityAmount");
        action.setParams({ oppId : component.get("v.recordId") });
        action.setCallback(this, function(response) {
            component.set('v.oppAmount',response.getReturnValue());
        }); 
        $A.enqueueAction(action);
    }
})

Result:

enter image description here

Related Topic