1

I am using $A.get('e.force:createRecord') on click of my lightning component's button. I saw the default behaviour is after save it redirects to that object's detail page.

Is there is any way out to override this behaviour?

0

3 Answers 3

4

you can use "panelOnDestroyCallback" function to redirect to a custom Url.

Please refer to the sample code:

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

    eve.setParams({
        "entityApiName": "Contact",
         "panelOnDestroyCallback": function(event) {
             window.location.href = "https://www.google.com";
    }
    });
    eve.fire();

}
1
  • Not working after Spring 19 Commented Jun 23, 2020 at 12:03
3

Not to my knowledge, asuming that you are having users fill in some kind of form, you should be better of using

lightning:recordEditForm

and use its available events, such as:

onsuccess Action The action triggered when the form is saved.

some of the advantages of this component are that it handles FLS, CRUD and is quite easy to use:

<lightning:recordEditForm objectApiName="sObjectName">
    <div class="slds-grid">
        <div class="slds-col slds-size_1-of-2">
            <!-- Your lightning:inputField components here -->
        </div>
        <div class="slds-col slds-size_1-of-2">
            <!-- More lightning:inputField components here -->
        </div>
    </div>
</lightning:recordEditForm>

and add your custom logic to do whatever it is you need done.

Otherwise, you can always Call a Server-Side Action

controller.js

"saveSomething" : function(cmp) {

    var action = cmp.get("c.saveSobject");
    action.setParams({ field: cmp.get("v.fieldName") });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            // custom action here 
        }
        else if (state === "INCOMPLETE") {
            // do something
        }
        else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });
    $A.enqueueAction(action);
}
3

In the Spring 19 above mentioned answers are not working and I tried something like below and it worked for me.

var eve = $A.get("e.force:createRecord");
eve.setParams({
    "entityApiName": "Contact",
    "navigationLocation" : "LOOKUP",
    "panelOnDestroyCallback": function(event) {
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
            "url": "/lightning/o/Contact/home"
        });
        urlEvent.fire();
    }
});
eve.fire();

It works fine after adding the "navigationLocation" : "LOOKUP",

0

You must log in to answer this question.

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