1
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');
        alert(currentUrl);

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

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 ?

5
  • 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
    Commented May 11, 2019 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(); } Commented May 11, 2019 at 8:37
  • For some weird reason I get nothing in window.location.hash Commented May 11, 2019 at 8:53
  • I think you cannot use panelondestroycallback function
    – RedDevil
    Commented May 11, 2019 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 Commented May 11, 2019 at 11:43

1 Answer 1

0

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.

4
  • 1
    Thank you, this is a perfect solution. Commented May 12, 2019 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!! Commented May 12, 2019 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? Commented May 12, 2019 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' . Commented May 12, 2019 at 15:55

You must log in to answer this question.

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