• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How To Redirect Record Page After Record Was Created In LWC

  • January 25, 2024
  • Tech Blogger
  • Merfantz Developments, Salesforce Developments Tutorial, Salesforce Lightning
  • 0

Introduction:

In this section, I will explain how to navigate to a record page in LWC. After creating a record on the Account, Redirect Record Page, where the next step is to navigate to the current UI page. The code is provided below.

HTML :

<template>
<lightning-card title=”How to Create Account With Contact In LWC” icon-name=”standard:account”>
<div class=”slds-grid slds-wrap”>
<div class=”slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium”>
<lightning-input label=”Name”
onchange={handleNameChange}
value={accountName}
name=”accountName”
class=”slds-m-bottom_x-small”></lightning-input>
</div>

<div class=”slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium slds-p-top_medium”>
<lightning-button label=”Save”
variant=”brand”
onclick={saveAction}></lightning-button>
</div>
</div>
</lightning-card>
</template>

JavaScript:

import { LightningElement, track } from ‘lwc’;
import { createRecord } from ‘lightning/uiRecordApi’;
import { ShowToastEvent } from ‘lightning/platformShowToastEvent’;
import { NavigationMixin } from ‘lightning/navigation’;
import accObj from ‘@salesforce/schema/Account’;
import nameFld from ‘@salesforce/schema/Account.Name’;

export default class HowToRedirectAccount extends NavigationMixin(LightningElement) {

@track accountName;
@track accountId;

handleNameChange(event) {
if (event.target.name == ‘accountName’) {
this.accountName = event.target.value;
}
}

navigateToRecord() {
this[NavigationMixin.Navigate]({
type: ‘standard__recordPage’,
attributes: {
recordId: this.accountId,
actionName: ‘view’
}
});
}

saveAction() {
const fields = {};
fields[nameFld.fieldApiName] = this.accountName;
const accRecordInput = { apiName: accObj.objectApiName, fields };
createRecord(accRecordInput)
.then(account => {
this.accountId = account.id;
this.dispatchEvent(
new ShowToastEvent({
title: ‘Success’,
message: ‘Account created’,
variant: ‘success’,
}),
);
this.navigateToRecord();
this.accountId=”;
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: ‘Error creating record’,
message: error.body.message,
variant: ‘error’,
}),
);
});
}
}

HTML (Template) Part:

1.<lightning-card> Component:
• Represents a card in the Salesforce Lightning Design System (SLDS).
Attributes:
• title: “How to Create Account With Contact In LWC”
• icon-name: “standard:account”
2.Container (<div>):
• SLDS grid and wrap classes (slds-grid slds-wrap).
• Contains two child <div> elements, each spanning the full width of the container.
3.Account Name Input Field (<lightning-input>):
• Allows users to input the name of the account.
• Attributes:
• label: “Name”
• onchange: Calls the handleNameChange method when the input value changes.
• value: Binds to the accountName property for two-way data binding.
• name: “accountName”
• class: SLDS styling class (slds-m-bottom_x-small) for a small bottom margin.
4.Save Button (<lightning-button>):
• Triggers the saveAction method when clicked.
• Attributes:
• label: “Save”
• variant: “brand” (indicating a primary action)
• onclick: Calls the saveAction method when the button is clicked.

JavaScript (Controller) Part
:
1.Import Statements:
• Import necessary modules from the Lightning Web Component framework.
• createRecord for creating a new record.
• ShowToastEvent for displaying toast messages.
• NavigationMixin for navigating to a record page.
• accObj and nameFld for referencing the Account object and its Name field.
2.@track Properties:
• accountName: Holds the name of the account.
• accountId: Holds the ID of the created account.
3.handleNameChange Method:
• Updates accountName based on user input in the name field.
4.navigateToRecord Method:
• Uses NavigationMixin to navigate to the view page of the newly created Account.
• Attributes:
• type: Specifies the type of navigation (standard__recordPage).
• attributes: Contains recordId (ID of the account) and actionName (set to ‘view’).
5.saveAction Method:
• Creates a new account record using createRecord and the provided account name.
• Displays a success toast message upon successful account creation.
• Calls navigateToRecord to navigate to the view page of the newly created account.
• Resets accountId to an empty string.
Error Handling (catch block):
• Displays an error toast message if there’s an issue creating the account.

Conclusion:

•The Lightning web component offers a cohesive solution for creating records and seamlessly navigating to the view page within the same UI, providing users with a streamlined experience.
•Incorporates best practices, including error handling, structured code, and effective use of Lightning base components and modules.
• This component is a valuable part of a Salesforce Lightning Experience solution for managing and interacting with records.

Redirect Links :

https://www.merfantz.com/blog/how-to-call-lightning-web-component-from-visualforce-page/

https://www.merfantz.com/blog/aura-components-vs-lightning-web-components/

Author Bio

Tech Blogger
+ Recent Posts
  • Comprehensive Overview of Salesforce Agent-Force Platform
    June 12, 2025
    Comprehensive Overview of Salesforce Agent-Force Platform
  • Salesforce GraphQL Revolutionizing Data Queries in CRM
    June 6, 2025
    Salesforce GraphQL: Revolutionizing Data Queries in CRM
  • Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
    May 29, 2025
    Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
  • Einstein Activity Capture Boosting Productivity & Relationship Intelligence
    May 22, 2025
    Einstein Activity Capture: Boosting Productivity & Relationship Intelligence
Tags: Merfantz TechnologiesPage Redirection using LWC
  • Previous New Features Included in Lightning Web Components
  • Next A Comprehensive Review of the Top 5 Field Service Software Options in 2024
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Comprehensive Overview of Salesforce Agent-Force Platform
Comprehensive Overview of Salesforce Agent-Force Platform June 12, 2025
Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025

Copyright @2023 Merfantz Technologies, All rights reserved