[SalesForce] ny way to define separate redirects for Cancel button and Save button on record creation screen

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");
        createRecordEvent.setParams({
            "entityApiName": "Opportunity",
            // Prepopulate values:
            "defaultFieldValues": {
                 "AccountId" : recordId //,
                 // Add more default values here if desired:
            },
            "panelOnDestroyCallback": function(event) {
                window.location.href = "https://www.google.com";
            },
            "recordTypeId":rtDet.id
        });
        createRecordEvent.fire();

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

Best Answer

I would suggest using the lightning:recordEditForm

<aura:component>
<lightning:recordEditForm aura:id="recordEditForm" recordTypeId="0123XXXXX"
                       objectApiName="Opportunity">
    <lightning:messages />
    <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: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}" />
</lightning:recordEditForm>

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.

Related Topic