Hyperlink a Record in lightning:datatable

Published in Troubleshooting Exceptions

With the Spring'21 release I found that custom navigation does not work correctly on few components in a salesforce community, it redirect to the error page instead of a record detail page.

Here is the code that reproduce the problem, such approach you can find in many other blogs:
<aura:component implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" access="global">
    <aura:attribute name="data" type="Object" access="private" />
    <aura:attribute name="columns" type="List" access="private" />

    <aura:handler name="init" value="{!this}" action="{! c.doInit }"/>

    <lightning:datatable
        keyField="id"
        data="{! v.data }"
        columns="{! v.columns }"
    />
</aura:component>
({
    doInit: function (component, event, helper) {
        component.set('v.columns', [
            {
                fieldName: "linkRecord",
                label: "Record Name",
                sortable: true,
                type: "url",
                typeAttributes: {
                    label: { fieldName: "recordName" },
                    target: "_parent"
                }
            }
        ]);

        const recordId = 'PUT_HERE_YOUR_RECORD_ID_FIELD_NAME';
        component.set('v.data', [
            {
                recordName: "Clickable link",
                linkRecord: "/" + recordId
            }
        ]);
    },
});
When I first time realized it I thought that it is salesforce release issue and I logged support case. But salesforce support replied that it works as design and there no official documentation regarding this change because salesforce not endorsed such refirects.

Salesforce Support comments
I have verified internally to check about the behavior of "/+recordId" and got to know that it is not an advisable approach due to its inconsistencies hence please use the Navigation Service which might involve additional development efforts but reduces the number of full app reloads, leading to better performance and consistency. Hence I have suggested using the below-mentioned approaches: lightning:navigation , force:navigateToURL

Coming to the difference in the behavior of lightning:formattedUrl and Anchor Tag: <a> cannot be compared as both are different and moreover using the href attribute of an <a> tag leads to inconsistent behavior in different apps and shouldn't be relied on. Please refer to the following Knowledge Article: Anchor Tag: <a>

I believe that hyperlinks with the format "/+recordId" used to work before Spring 21 but it is not endorsed by Salesforce therefore there is no official documentation.

How it can be updated?

Useful links:
Tagged under: Spring'21 Community

Comments powered by CComment