TIP OF THE WEEK: How to redirect or perform action after force:createRecord form submit

In Salesforce Lightning platform we use different lightning events to communicate between components. One such event is force:createRecord which enables calling salesforce lightning record creation from custom lightning components/apps. This becomes handy as it saves the effort of building custom components to handle the record creation process.

 

In force:createRecord there isn’t a straight forward way to perform a redirect another action after modal form submission as per Lightning component library documentation.

 

Therefore to overcome this limitation we found the below workaround. You can use it to perform the desired action upon submitting record creation form by the user.

 

For example: 

 

        var forceCreateRecordEvent = $A.get("e.force:createRecord");

        forceCreateRecordEvent.setParams({
            "entityApiName": "Opportunity",
            "navigationLocation": "LOOKUP",
            "panelOnDestroyCallback": function(event) {
                $A.get('e.force:refreshView').fire();              // Refresh view after Record creation form submitted
                
                // OR navigate to a different lightning page or external URL, etc.
                /*var urlEvent = $A.get("e.force:navigateToURL");
                
                if(urlEvent) {
                    urlEvent.setParams({
                        "url": "/lightning/n/myLightningTab"
                    });
                    
                    urlEvent.fire();
                }*/

            }
        });

        forceCreateRecordEvent.fire();


Note: You will need to use navigationLocation attribute and panelOnDestroyCallback event to perform this.

Learn how to connect Components with Events

Read more on Lightning component events & Event propagation

Follow latest posts on #Lightning topics in our blog

Get in touch today to see how WDCi can help your business.