Wednesday, September 8, 2021

LWC NavigationMixin - Redirect to parent record and LWC Headless action redirection

1. Reference link for LWC Headless action:

NOTE: This headless action is supported only on the standard record page. - Not worked for community's ref link:  

https://help.salesforce.com/s/articleView?language=en_US&type=5&release=232&id=release-notes.rn_lwc_quick_actions.htm


2. Navigate to LWC or Aura component with RecordId

handleAction(event) {
  var RecIdJSON.stringify(event.detail.row.Id);
    this[NavigationMixin.Navigate]({
        "type": "standard__component",
        "attributes": {
            "componentName": "c__TestForm" //if LWC component : c___test_form
        },
        state: {
            c__recordId: JSON.parse(RecId)
        }
    });
}
(OR)
    renderedCallback() {
        this.sfdcBaseURL = window.location.origin;
    }
 
    handleAction(event) {
var RecIdJSON.stringify(event.detail.row.Id);
       this[NavigationMixin.Navigate]({
                 "type": 'standard__webPage',
                 "attributes": {
                   "url": this.sfdcBaseURL+'/lightning/cmp/c__TestForm?c__recordId='+JSON.parse(RecId)
                 }
             });
    }


3. Sample Redirection to parent record:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
        <target>lightning__RecordAction</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
            <actionType>ScreenAction</actionType>
        </targetConfig>
        <targetConfig targets="lightning__RecordPage">
            <objects>
                <object>Contact</object>
            </objects>
        </targetConfig>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" type="String" label="Record ID" 
description="Should be set to {!recordId}" default="{!recordId}"/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

 Template:

<template>
    <div class="slds-clearfix">
        <div class="slds-float_right">
            <lightning-button label="Return to Roadmap" 
onclick={navigateToViewAccountPage}></lightning-button>
        </div>
    </div>
</template>


JS

  import { LightningElementapiwire } from 'lwc';
  import { getRecord } from 'lightning/uiRecordApi';
  import { NavigationMixin } from 'lightning/navigation';
  
  const FIELDS = ['Contact.Account.Name',
'Contact.Account.Id']; 

  export default class ReturnToRoadmapLWC extends NavigationMixin (LightningElement){
      @api recordId;  

      connectedCallback(){
        console.log('recordid========='+this.recordId);
      }

      //Wire the output of the out of the box method getRecord to the Contact object
      @wire(getRecord, { recordId: '$recordId'fields: FIELDS })
      Contact;
  
      //Navigate to Account record from Contact
      navigateToViewAccountPage(event) {
        var AccountName this.Contact.data.fields.Account.value.fields.Name.value;
        var AccountId this.Contact.data.fields.Account.value.fields.Id.value;            
          console.log('Contact Account name ========='AccountName);
          console.log('Contact Account ID ========='AccountId);
        this[NavigationMixin.Navigate]({
            type: 'standard__webPage',
            attributes: {
              url: '/'+AccountId
                //url: '/hello/s/testurl/'+AccountId+'/'+AccountName
            }
        });
      }
  }

No comments:

Post a Comment

CSS Styling

Styling: 1. Inline Styling: < div style = "font-size: 2rem;font-weight: 400;" > Good Evening </ div > 2. Usi...