[SalesForce] Redirect to listing/record page onclick on Cancel button or Save save button LWC

enter image description here

Iam working on LWC component.
On NEW button click LWC component opened.
Enter data on fields and click on 'Create Account' button, it should redirect/navigate user to 'listing/record' page.
OR click on cancel button it should redirect user to 'listing/record' page.

right now when click on 'Create Account' button, data added successfully with no redirection/navigation to listing page.
Cancel button only resting the fields.

Thanks in Advance.

using this sample code for save and cancel

Best Answer

NavigationMixin controls navigation to Salesforce pages or the creation of URLs to those pages. You can go to edit, view, etc. Here is the documentation: https://developer.salesforce.com/docs/component-library/bundle/lightning-navigation/documentation.

Here is the js class, notice the info in "header", it is really important:

import { NavigationMixin } from "lightning/navigation";
import { CloseActionScreenEvent } from "lightning/actions";  //if you want to close a page, but you don't need to if you navigate

export default class btnCreateShipment extends NavigationMixin(
  LightningElement
) {

    navigateToListView() {
        // Navigate to the Contact object's Recent list view.
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Contact',
                actionName: 'list'
            },
            state: {
                // 'filterName' is a property on the page 'state'
                // and identifies the target list view.
                // It may also be an 18 character list view id.
                filterName: 'Recent' // or by 18 char '00BT0000002TONQMA4'
            }
        });
    }

    navigateToRecordEditPage(recordId) {
        // Opens the record modal
        // to view a particular record.
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: recordId,
                actionName: 'edit'
            }
        });
    }
}

Finally, I would suggest you set up LWC Recipes, if you haven't already... many of the basic things you will want to do are built out. This will save you hours and hours of time: https://github.com/trailheadapps/lwc-recipes

Good luck!

Related Topic