[SalesForce] Refresh parent lightning component detail page from embedded lightning component

I have a lightning component in a custom lightning record detail page. I want to force refresh on the parent page when the lightning component is loaded. i.e.

  1. I open a record.

  2. Record detail page loads- this is the custom lightning record detail page.

  3. Custom lightning component embedded at the bottom of the page loads.

  4. In the custom lightning embedded component, I have the following piece of code. (within helper)

    updateAssignedTo : function(component, event, helper) {
    var recordId = component.get("v.recordId");
    console.log('Record Id&&&&&&****'+recordId);
    var action = component.get("c.updateAssignedTo");
    
    action.setParams({
        accountRequestId : recordId,
    });
    
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
          **$A.get('e.force:refreshView').fire();**
        }else if (state === "ERROR") {
    
        }
    });
    $A.enqueueAction(action);
    },
    

var action = component.get("c.updateAssignedTo"); is successfully called.

I am trying the refresh the parent page by the following piece of code.

$A.get('e.force:refreshView').fire();

However, it is not happening.

Is this a feasible way to do it?

Best Answer

Try using lightning data service, and then call a controller method in the "recordUpdated" attribute. Something like this-

Component

<aura:attribute name="oppRecord" type="Opportunity" />
<aura:attribute name="recordId" type="Id" />

<force:recordData aura:id="dataService"
                      recordId="{!v.recordId}"
                      targetFields="{!v.oppRecord}"
                      fields="Id,Name,StageName,..(whatever you want, or use the layout)"
                      layoutType="FULL"
                      mode="EDIT"
                      recordUpdated="{!c.handleRecordUpdated}"/>

Javascript Controller

handleRecordUpdated : function(component, event, helper) {
    // refresh the page with the record updates
    $A.get('e.force:refreshView').fire();
},