Pop up standard record View/Edit page from LWC Datatable or Tree Grid row
- How to open a standard record view page from the datatable/tree grid row in lwc?
- How to pop up the standard record edit page from the datatable /tree grid row in lwc?
Expected Output
I will demonstrate this on the Opportunity object in salesforce. Before I
expose the final code to you here is the brief
step by step explanation of my code.
- Write an apex method to fetch data from the object.
- Use standard datatable/tree grid component.
- Call the apex method using a wired method or property.
-
Import and extend the navigation mixin in that component.
import { LightningElement, wire } from "lwc"; import { NavigationMixin } from "lightning/navigation"; // ... export default class DatatableNavigationDemo extends NavigationMixin(LightningElement) { // ... your code. }
-
Add a column definition like,
type="button"
in the data table column list. Below is the sample definition for the Name button column.
{ label: "Name", type: "button", typeAttributes: { label: { fieldName: "Name" }, name: "gotoOpportunity", variant: "base" } }
The Edit button column definition is as below.
{ label: "Edit", type: "button", typeAttributes: { label: "Edit", name: "editOpportunity", // this is name is used to identify // the action in action handler variant: "brand" } }
-
Add the row action handler to the lightning data table/tree grid.
I have used NavigationMixin.GenerateUrl to generate the URL of the record, and opened that in a new tab usingwindow.open()
method in js.
this[NavigationMixin.GenerateUrl]({ type: "standard__recordPage", attributes: { recordId: event.detail.row.Id, actionName: "view" } }).then((url) => { window.open(url, "_blank"); });
I have used NavigationMixin.Navigate to pop up the standard record edit page of the record.
this[NavigationMixin.Navigate]({ type: "standard__recordPage", attributes: { recordId: event.detail.row.Id, actionName: "edit" } });
Note: All the code explained here also work for Lightning Tree Grid Component
Final Code
I have written a simple apex method to query closed opportunities.
Apex Code
public with sharing class DatatableNavigationDemoController { @AuraEnabled(cacheable=true) public static List<Opportunity> getOpportunities(){ return [SELECT Name, StageName, Amount, CloseDate, Description FROM Opportunity WHERE StageName = 'Closed Won' LIMIT 20]; } }
I have written below HTML code. Note that I have added a row action handler
named
handleRowAction
.
HTML Code
<template> <lightning-card variant="Narrow" title="Hello" icon-name="standard:opportunity"> <p class="slds-p-horizontal_small"> <lightning-datatable if:true={opportunities.data} key-field="id" data={opportunities.data} show-row-number-column hide-checkbox-column columns={opportunityCols} onrowaction={handleRowAction}> </lightning-datatable> </p> </lightning-card> </template>
JS Code
import { LightningElement, wire } from "lwc"; import getOpportunities from "@salesforce/apex/DatatableNavigationDemoController.getOpportunities"; import { NavigationMixin } from "lightning/navigation"; const OPPORTUNITY_COLS = [ { label: "Name", type: "button", typeAttributes: { label: { fieldName: "Name" }, name: "gotoOpportunity", variant: "base" } }, { label: "Stage", fieldName: "StageName" }, { label: "Amount", fieldName: "Amount", type: "currency" }, { label: "Close Date", type: "date", fieldName: "CloseDate" }, { label: "Description", fieldName: "Description" }, { label: "Edit", type: "button", typeAttributes: { label: "Edit", name: "editOpportunity", variant: "brand" } } ]; export default class DatatableNavigationDemo extends NavigationMixin(LightningElement) { opportunityCols = OPPORTUNITY_COLS; @wire(getOpportunities, {}) opportunities; handleRowAction(event) { if (event.detail.action.name === "gotoOpportunity") { this[NavigationMixin.GenerateUrl]({ type: "standard__recordPage", attributes: { recordId: event.detail.row.Id, actionName: "view" } }).then((url) => { window.open(url, "_blank"); }); } if (event.detail.action.name === "editOpportunity") { this[NavigationMixin.Navigate]({ type: "standard__recordPage", attributes: { recordId: event.detail.row.Id, actionName: "edit" } }); } } }
Follow this blog if you get regular updates for more such stuff in SFDC.
Happy learning!!
thank you so for this code
ReplyDeleteThank you, dear, Unknown!
Deletefor me the page is navigated to the record from datatable which i dont want
ReplyDeleteHi Dear Unknown, just remove the "_blank" value from the window.open method, so it will not navigate to new page.
DeleteHow to use refresh apex to refresh the datatable dynamically I mean without hard refresh
ReplyDeleteThank you
Hi Dear Unknown, you can use the imperative method in that case.
DeleteHi I don't want the user to have any edit permission when user clicks on record but for edit button we have to edit...Also Can we filter these 'Edit button' to get populated only on based on the Status values?
ReplyDeleteThank you!
ReplyDeleteHi Chaitanya, yes you can do that with dynamic row actions. Check this out:
ReplyDeletehttps://salesforce.stackexchange.com/questions/310932/dynamic-row-actions-in-data-table-using-lwc