I've built a lightning component that runs when the user clicks the standard 'New' button on the Opportunities related list on Account. My component invokes e.force.createRecord meaning it shows the Opportunity record creation modal when ran.

The Opportunity record creation modal has 3 buttons (Cancel, Save and New, and Save). Is there a way to define separate redirects for Cancel and Save? Currently I can define one redirect that gets applied to both the Cancel and Save button, via the following code:

var createRecordEvent = $A.get("e.force:createRecord");
            "entityApiName": "Opportunity",
            // Prepopulate values:
            "defaultFieldValues": {
                 "AccountId" : recordId //,
                 // Add more default values here if desired:
            "panelOnDestroyCallback": function(event) {
                window.location.href = "https://www.google.com";

Here's an image to better illustrate my dilemma: enter image description here


I would suggest using the lightning:recordEditForm

<lightning:recordEditForm aura:id="recordEditForm" recordTypeId="0123XXXXX"
    <lightning:messages />
    <div class="slds-grid">
        <div class="slds-col slds-size_1-of-2">
            <!-- Your lightning:inputField components here -->
        <div class="slds-col slds-size_1-of-2">
            <!-- More lightning:inputField components here -->
    <lightning:button class="slds-m-top_small" type="submit" label="Create new" />
    <lightning:button class="slds-m-top_small" type="Cancel" label="Cancel" onclick="{!c.customAction}" />

and then leverage the events provided by the component such as :

onsuccess --> The action triggered when the form is saved.

To do your custom action and for your cancel button, you can define your own method.


Add the param "navigationLocation": "RELATED_LIST" to prevent the event from navigating away after saving.

       "entityApiName" : "Contact",
       "navigationLocation": "RELATED_LIST"

The following solution was given by Dmytro Kryvyi: which actually in the post suggested by @kurunve.

*"There is an undocumented solution for this. Below is the js controller function that creates a new Contact record and redirects back from the record page to the initial page after a new record is created.

createNewContact: function(component, event, helper) {
        var windowHash = window.location.hash;
        var createEvent = $A.get("e.force:createRecord");
            "entityApiName": "Contact",
            "panelOnDestroyCallback": function(event) {
                window.location.hash = windowHash;

Where "panelOnDestroyCallback" is the hidden force:createRecord attribute. I know that's an ugly hack, but hey, if I had known this before I wouldn't have spent 50+ hours implementing a fully-custom multi-object 'createRecord' component with all the schema tricks, custom lookups, multipicklists etc."*

  • windowHash is undefined when i tried this. Jul 20 '18 at 17:26
  • Do you have anything after '#' in the url, if you dont have anything after hash you will get undefined. you could also do window.location.hash = "someIdinsfdc"; then it will take you to that record. Jul 20 '18 at 17:35
  • Here is the URL: ronny555-dev-ed.lightning.force.com/lightning/o/Opportunity/… As you can see, no #. Even if there was, I don't see a way of setting one URL for Cancel, and a totally different one for Save. Jul 20 '18 at 17:39

Sorry to be the bringer of bad news, but this is not possible at the time of posting this answer. I am afraid you must resort to using force:recordData as described here and customize the entire modal to fit your requirement.

EDIT: You can set defaults upon init like so:


<aura:attribute name="record" type="Object"/>
<aura:attribute name="newRecord" type="Object"/>
<aura:attribute name="recordError" type="String"/>

<aura:attribute name="recordId" type="String"/>
<aura:attribute name="sObjectName" type="String"/>

<force:recordData aura:id="myOpportunity"

<aura:handler name="init" value="{!this}" action="{!c.initializeRecord}"/>


initializeRecord: function(component, event, helper) {

            var sObjectName = component.get("v.sObjectName");

            var recordDataAuraId = 'myOpportunity';

                    $A.getCallback(function() {
                        var newRecord = component.get("v.newRecord");
                        var error = component.get("v.recordError");
                        if(error || (newRecord === null)) {
                            console.log("-------> Error initializing record template: " + error);
                            var parentRecordId = component.get("v.recordId"); //set parent record Id here
                            newRecord.fields.Parent_Lookup_Field_API_Name_Here__c.value = parentRecordId;
                            // add more defaults as necessary
        }catch(err) {
            console.log('-------> ERROR: ' + err + ' ** MESSAGE: ' + err.message + ' ** STACK: ' + err.stack);
  • I'd use force:recordData, but it doesn't appear there's a way to default lookup fields on the record creation modal. Jul 20 '18 at 17:25
  • You can default lookup fields. Please see updated answer. Jul 20 '18 at 17:47
  • Can force:recordData be combined with force:recordForm? Jul 20 '18 at 20:38

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.