handleCreateRecord : function(component, event, helper) {

        let currentUrl = decodeURIComponent(window.location.search.substring(1));
        let createRecordEvent = $A.get("e.force:createRecord");
        var accountId = component.get('v.accountId');

            "entityApiName": "Lead",
            "defaultFieldValues": {
                "Account__c" : accountId,
                "RecordTypeId": "0121l000000566UAAQ"
            "panelOnDestroyCallback":function(event) {
                var navEvt = $A.get("e.force:navigateToSObject");
                    "recordId": "0011l000006pDoxAAE",
                // navigate back to account only if Lead create was cancelled
                decodeURIComponent(window.location.search.substring(1)) == currentUrl ? navEvt.fire() : console.log('nav to Lead');

Okay, So..I am trying to make the user go back to the account page upon hitting the cancel in this new creation of my custom object but it is not working. May I know what's wrong here ?

  • Just a side note 'panelOnDestroyCallback' function is a hack you are using and not a documented solution. Check this for more information. It seems to have stopped working after summer 2019. SF Issue
    – RedDevil
    May 11 '19 at 8:00
  • I have tried the other way like the link you have suggested, but it did not work either :/ createNewContact: function(component, event, helper) { var windowHash = window.location.hash; var createEvent = $A.get("e.force:createRecord"); createEvent.setParams({ "entityApiName": "Contact", "panelOnDestroyCallback": function(event) { window.location.hash = windowHash; } }); createEvent.fire(); } May 11 '19 at 8:37
  • For some weird reason I get nothing in window.location.hash May 11 '19 at 8:53
  • I think you cannot use panelondestroycallback function
    – RedDevil
    May 11 '19 at 9:39
  • I was wondering the same, cause how the function know that upon hitting cancel what to hit cause normally when we write custom buttons we explicitly call the cancel function and close the modal dialog but here, I have no idea May 11 '19 at 11:43

As of API v43, the suggested method for navigation is using lightning:navigation and you can see examples in the documentation here. Note that it leverages the PageReference object and you should use standard_recordPage type in this situation.

Also, Lightning Locker wraps the original window and makes it a SecureWindow. As per the documentation here, it looks like location has different behavior than the standard window.location that you're used to.

Last, you can try the Lightning Console with sample code, inspect in the JS console and see what values it gives you.

  • 1
    Thank you, this is a perfect solution. May 12 '19 at 13:16
  • So, I have tried this individually using a button, it works but I want to use in the force:createRecord. Well, apparently panelOnDestroyCallback is not working and then navigation never gets called. Doomed!! May 12 '19 at 14:16
  • Worst case scenario, you can use an Apex controller for the aura component to do the simple save and then do the navigation after a successful save. I don't see panelOnDestroyCallback in the documentation - did you see that method used somewhere else? May 12 '19 at 14:50
  • I have not seen in the documentation but I have some posts where it worked apparently some time ago and I want my component to go back to the type 'Standard_recordPage' with the actionname 'view' when user clicks on 'Cancel' . May 12 '19 at 15:55

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.