Override e.force:createRecord default behaviour on cancel - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Subhasree KonarSubhasree Konar 

Override e.force:createRecord default behaviour on cancel

Hi,

I have used e.force:createRecord to create record with some default values to prepopulate. But when the createRecord page opens and i try to click on cancel or close the form its not redirecting to my object Home. Is there any workaround or way possible to redirect to Object home page whenever i click on cancel or close? 
Here is my code snippet
RecordCreation : function(component,recTypeId){
         if(recTypeId != null) {
           document.getElementById("newClientSectionId").style.display = "none" ;
           
            var createRecordEvent = $A.get("e.force:createRecord");
            //console.log('Its working');
            createRecordEvent.setParams({
                "entityApiName": "Object__c",
                "recordTypeId":recTypeId,               
                "defaultFieldValues": component.get("v.selectionMap"),
              
        
                
            });
            createRecordEvent.fire();
         }
    }

Its currently redirecting to blank because i used 
document.getElementById("newClientSectionId").style.display = "none" ;

If there is any way to redirect to object home on click of cancel let me know. TIA!
 
sakshi nagpalsakshi nagpal
Hi Subhasree,

 You can navigate to object home this way-:
https://developer.salesforce.com/docs/component-library/bundle/force:navigateToObjectHome/documentation

 Thanks,
Sakshi
Subhasree KonarSubhasree Konar
Hi Sakshi,
could you please elaborate on more like how to use the navigatetoHome event on particularly clicking on cancel button. I have tried but it navigating to home once I click on save not on cancel. I want to navigatetoHome only clicking on cancel or close.

Thanks.
sakshi nagpalsakshi nagpal
Hi Subhashree,

 I would suggest using the lightning:recordEditForm
 Something like this-:
<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 you can add custom action for Cancel button.

on onclick of cancel call custom method that takes you to object home page as mentioned here-:
https://developer.salesforce.com/docs/component-library/bundle/force:navigateToObjectHome/documentation


 Thanks,
Sakshi