5

I'm using $A.get('e.force:editRecord') to display standard lightning edit modal window. I want to refresh my component after user saves the given record.

However there are no callback events documented by salesforce on record save success for e.force:editRecord.

By using chrome Lightning inspector tool I found two events getting fired when I clicked on Save button.

1. force:recordChange (Application Event)
2. force:save (Component Event)

I declared an application event handler for force:recordChange

<aura:handler event="force:recordChange" action="{!c.handleRecordChange}"/>

Got following error

[ERROR]: No EVENT named recordChange found: Source

To handle component event force:save I have to specify event name

<aura:handler name="?" event="force:save" action="{!c.handleRecordSave}"/>

How do I notify custom component when user saves the record.

3
  • To refresh a view, run $A.get("e.force:refreshView").fire();, which reloads all data for the view.
    – SE_User
    Commented Feb 8, 2017 at 13:02
  • As mentioned above, you can use force:refreshView for that but I think the event you're looking for is force:recordSaveSuccess. Doc: developer.salesforce.com/docs/atlas.en-us.lightning.meta/… Commented Feb 8, 2017 at 15:51
  • @Soham did you ever get a solution for this? Commented Jan 7, 2019 at 9:56

3 Answers 3

5

You can inject your handler by using

component.addEventHandler("force:recordChange", component.getReference("c.handlEvent"));

It will work, but you can't prevent any 'default' phase events that you will catch

1
  • 2
    That's a great idea
    – MadMax
    Commented Apr 26, 2019 at 21:28
3

Been looking for such solution for a while. Seems like I'm not the only one and currently I had to settle for a workaround - I know it's not pretty and might be considered as kinda of a hack - but it's working.

In my Parent component I've placed :

 <!-- toast capture the RecordEdit Success/Warning notifications events  -->
 <aura:handler event="force:showToast" action="{!c.refreshAll}"/>

 <!-- My Child component that need to be refreshed from server -->
 <c:myChildComponent  rootObjectId="{!v.recordId}" aura:id="compGroup"/> 

Parent JS Controller

refreshAll: function (component, event, helper) {
        var eventType = event.getParam('type');
        var indexCall = 0; // to make it call only 1 time
        if(eventType == 'SUCCESS'){
        if(indexCall == 0){
            indexCall +=1;
         var myComp = component.find('compGroup');
         if(myComp)
         var auraMethodResult = myComp.refresh();    
        }
       }
    },

Child Component Method

   <aura:attribute name="rootObjectId" type="String" description="root record Id"/>
   <aura:attribute name="data" type="Object" description="results records"/>

   <aura:method name="refresh" action="{!c.refreshResults}" access="public">
            <aura:attribute name="rootObjectId" type="String" />
    </aura:method>

Child JS controller - calls Apex Method action to get new fresh data

  // init any value which need to be refreshed     
  refreshResults: function(component, event, helper)  {
       var rootObjectId = component.get("v.rootObjectId");
        var action = component.get("c.getMyData");
        action.setParams({ 
                'contextId': rootObjectId
            });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") { 
                var data = JSON.parse(response.getReturnValue());

            }
        }
   },
1

I can solve it using

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

and later, when the view is refreshed, you can "delete" browser calls calling the same url plus some random param. Something like that

 var idObject= component.get("v.recordId");
        action.setParams({ id : component.get("v.recordId") });
        action.setCallback(this, function(response){           
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS"){
                 setTimeout(function() {
                 $A.get('e.force:refreshView').fire();
                     setTimeout(function() {
                        var url = window.location.href.split('?')[0];
                        var chars = "0123456789abcdefABCDEF";
                        var lon = 20;
                        var code = "";
                        for (var x=0; x < lon; x++)
                        {
                            var rand = Math.floor(Math.random()*chars.length);
                            code += chars.substr(rand, 1);
                        }
                        window.location.href=url+'?uncached='+code; 
                     }, 2000);                
                }, 3000);
            }   
        });       
        $A.enqueueAction(action);
4
  • Can you please add a bit more explanation?
    – Adrian Larson
    Commented Oct 16, 2017 at 19:57
  • setTimeout should be a last option as in future you would want to have more control over on whats exactly is going on.
    – Raul
    Commented Oct 16, 2017 at 20:02
  • 1
    LOL to make it worse this is a double timeout
    – Rao
    Commented Oct 16, 2017 at 20:33
  • The whole situation is: I need a Quick Action which call Google Maps API with a Direction for get Coordenades. This is the action called from callback; I wait for three seconds for the Webservice, and for the update operation. After three seconds I refresh the view. Finally I wait two seconds for the view refresh, and refresh the whole page without cache calling the same URL adding the random param. Commented Oct 16, 2017 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .