5

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.

  • How about firing event when child record is added and handling that event and updating totals on the master record. Did you try that? – javanoob Nov 7 '17 at 16:06
  • @javanoob : Someone on our team looked into it, but where would "handling the event", as you say, happen? As far as I can tell, we'd have to build a custom component to replace "Related List", in order to capture the "child record is added" event. – shannon Nov 7 '17 at 20:14
  • 1
    My mistake. I thought related list is a custom component. Sorry! – javanoob Nov 7 '17 at 21:03
3

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

  • So basically you are just handling showToast. Definitely a hack, but clever! I think I'm willing to use this for the moment. Will try it out, and assess what else is likely to generate toasts from the same page. I'm especially concerned about possible update loops. I've upvoted; if it works out I'll also mark as answer. – shannon Nov 7 '17 at 22:11
  • @shannon You can replace showToast with refreshView if that suites your requirement better. refreshView event also working fine. – javanoob Nov 7 '17 at 23:12
  • I mentioned in my question above that refreshView is not working for us. Our project is extremely minimal, in a fresh Scratch Org. Is that similar to your test scenario? – shannon Nov 8 '17 at 2:54
  • Ignore my comment about refreshView then..sorry 😀 – javanoob Nov 8 '17 at 2:57
  • Did you try refreshView? Because that is where we started our search, and I'd like to know if it worked for someone else. – shannon Nov 8 '17 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.