Activity Forums Salesforce® Discussions Refresh Lightning component on Opportunity record Edit Save (Standard Layout) Reply To: Refresh Lightning component on Opportunity record Edit Save (Standard Layout)

  • Piyush

    Member
    April 25, 2016 at 11:10 am

    Hello Danna,

    Here is the code from my test app. My use case was in context of the Accounts and contacts but it should apply to any object. As far as my understanding when ever an object is saved then the current view is refreshed. This is visible by the spinner that is displayed over the top panel of the object. I added a handler to my component that uses the force:refreshView (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_refreshView.htm) and just recalled my init method but could call another method if need be. This basically ensured that any time the object that was hosting the component was refreshed that my component would also refresh as well.

    Here is the Component Code:

    <aura:component controller="editRecordSimulationController"
    implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,force:hasSObjectName">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:dependency resource="markup://force:editRecord" type="EVENT" />
    <aura:handler event="force:refreshView" action="{!c.doInit}" />
    <aura:attribute name="recordId" type="string" />
    <aura:attribute name="accType" type="String" />
    <aura:attribute name="accObj" type="account" default="{ sObjectType: 'Account'}"/>
    <ui:inputText label="Record Id" value="{!v.recordId}" required="true"/>
    <ui:button class="btn" label="Submit" press="{!c.setOutput}"/>

    <br />
    <br />
    <br />
    Account Type: <ui:outputText value="{!v.accObj.Type}" />
    </aura:component>

    Here is my Controller Code:

    ({
    doInit : function(component, event, helper) {
    var recordId = component.get("v.recordId");
    var action = component.get("c.getTypeFromAccount");
    action.setParams({
    recordId: recordId
    });
    action.setCallback(this, function(response){
    var state = response.getState();
    if (state === "SUCCESS") {
    var acc = response.getReturnValue();
    component.set("v.accType", acc.Type);
    component.set("v.accObj", acc)
    }
    });

    $A.enqueueAction(action);
    },
    setOutput : function(component, event, helper) {
    var editRecordEvent = $A.get("e.force:editRecord");
    editRecordEvent.setParams({
    "recordId": component.get("v.recordId")
    });
    editRecordEvent.fire();
    }
    })

    Here it the APEX controller code:

    public class editRecordSimulationController {

    @AuraEnabled
    public static Account getTypeFromAccount(string recordId)
    {
    Account acc = [select Name, Type from Account Where Id = :recordId limit 1];
    return acc;
    }
    }