4

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.

2

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());

            }
        }
   },
2

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

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);
  • Can you please add a bit more explanation? – Adrian Larson Oct 16 '17 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 Oct 16 '17 at 20:02
  • 1
    LOL to make it worse this is a double timeout – Rao Oct 16 '17 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. – Edu Trujillo Oct 16 '17 at 21:26

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.