Override standard button with lightning component

All we need is an easy explanation of the problem, so here it is.

I have a custom Object, I was trying to override the NEW standard button with lightning component.

On Click of New button , below modal popup is opened and save the Data is working.

Issue: On Click of cancel button I’m just calling cancelDialog method in js. Expected is to close the modal. but It’s doesn’t do anything, It still remains in same Page.

Can anyone Please help me on this.

cancelDialog : function(component, helper) {      
    var home = $A.get("e.force:navigateToObjectHome");     
    home.fire();
},

Component

<aura:attribute name="items" type="NewData__c[]"/>
<aura:attribute name="enroll" type="NewData__c" default="{'sobjectType': 'NewData__c',
                                                            'NewData_Name__c':'',
                                                            'NewData_Email__c':''}" />    



<section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
    <div class="slds-modal__container">

        <header class="slds-modal__header">
            <lightning:buttonIcon iconName="utility:close"
                                  onclick="{!c.cancelDialog}" 
                                  alternativeText="close"
                                  variant="bare-inverse"
                                  class="slds-modal__close"/>
            <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">New Enrollment</h2>
        </header>

        <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
            <lightning:input   aura:id="field" messageWhenValueMissing ='Name cant be blank.'   required="true" value="{!v.enroll.NewData_Name__c}"
                             label="Name" />                
            <lightning:input type="email"   aura:id="field" messageWhenValueMissing ='Email cant be blank.'  required="true" value="{!v.enroll.NewData_Email__c}" label="Email" />
        </div>
        <footer class="slds-modal__footer">
            <lightning:button variant="neutral" label="Cancel" onclick="{!c.cancelDialog}" />
            <lightning:button variant="brand" label="Save" onclick="{!c.createData}" />
        </footer>
    </div>
</section>
<div class="slds-backdrop slds-backdrop_open"></div>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

To navigate to an object home, set the object name on the scope attribute and fire the event.

Below will displays the home page for a custom object.

cancelDialog : function(component, helper) {      
        var home  = $A.get("e.force:navigateToObjectHome");
        home .setParams({
            "scope": "NewData__c"
        });
        home .fire();
    }

Please refer the below link:

https://developer.salesforce.com/docs/component-library/bundle/force:navigateToObjectHome/documentation

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply