Dynamic href to list of records in LWC Salesforce

Dynamic href to list of records in LWC Salesforce

LWC:

HTML:

<template>

    <div class="slds-box slds-theme--default">
        <template if:true = {records}>                  
                <div style = "height: 300px;">
                    <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped">
                        <tr>
                            <th scope = "col">Name</th>
                            <th scope = "col">Industry</th>
                            <th scope = "col">Account Number</th>
                        </tr>
                        <template iterator:it = {records}>
                            <tr key = {it.value.Id}>
                                <td><a href={it.value.hrefVal}>{it.value.Name}</a></td>
                                <td>{it.value.Industry}</td>
                                <td>{it.value.AccountNumber}</td>
                            </tr>
                        </template>
                    </table>
                </div>                  
     
            </template>      
            <template if:true = {error}>  
                {error}>                  
            </template>  
    </div>
    
</template>

JavaScript:

import { LightningElement, wire } from 'lwc';
import fetchAccounts from '@salesforce/apex/AccountController.fetchAccounts';
import { NavigationMixin } from 'lightning/navigation';

export default class Sample extends NavigationMixin( LightningElement ) {

    records;
    error;

    @wire( fetchAccounts )  
    wiredAccount( { error, data } ) {

        if ( data ) {

            let rows = [];
            let tempRows = JSON.parse( JSON.stringify( data ) );

            for ( let i = 0; i < tempRows.length; i++ ) {

                let row =  tempRows[ i ];
                console.log( "Element value is " + JSON.stringify( row ) );
                row.hrefVal = "/apex/Example?recId=" + row[ "Id" ] + "&checkBool=true";
                rows.push( row );
                
            }

            this.records = rows;
            this.error = undefined;

        } else if ( error ) {

            this.error = error;
            this.records = undefined;

        }

    }

}

JavaScript-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>49.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__Tab</target>
    </targets>
</LightningComponentBundle>

Visualforce page:

<apex:page >
    
    {!$CurrentPage.parameters.recId}<br/>
    {!$CurrentPage.parameters.checkBool}
    
</apex:page>

Output:

Leave a Reply