1

With this code, I want the moment I click on the button the user moves to the account object (redirection from obj__c to account_c).

  • obj__c has a Master-Detail relation with the Account .

  • When adding each record of obj__c , we indicate associated account .

I don't get any error but I know that there is a problem in my code because when I use in recordId an account id value, the redirection works correctly.

=> Apex

public with sharing class objClass{
    @AuraEnabled(cacheable=true)
    public static obj__c MyMethode(Id objId) {
            return[SELECT id ,Account__r.id FROM obj__c WHERE id=:objId LIMIT 1];
        
}
}

=> JS

import { LightningElement, api , wire, track } from 'lwc';
import IdAccount_FIELD from '@salesforce/schema/obj__c.Account__r.id'; 
import MyMethode from '@salesforce/apex/objClass.MyMethode ';
import { NavigationMixin } from "lightning/navigation";

export default class RedirectToPage extends NavigationMixin (LightningElement) {

    @api recordId;

    @wire(MyMethode,{ objId: '$recordId'}) acc;

    get MyId() {
        return this.acc.data  ? getSObjectValue(this.acc.data, IdAccount_FIELD) : '';        
    }

    navigateToAccountPage() {
    this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.MyId,       
                objectApiName: 'Account',
                actionName: 'view'
                 }
        });
   
    }

}

=> HTML

<template>  
    <div class="slds-box slds-theme_shade">  
      <lightning-button label="Back to Account" onclick={navigateToAccount}> </lightning-button>  
    </div>  
  </template>  
8
  • What happens when you call the method this.MyId() instead of this.MyId? Also are you able to debug what is there in this.MyId? Jan 2, 2023 at 14:55
  • @MohithShrivastava , I don't get any error and also I am not redirected to the page , the request is correct , I tried it in the inspector , but when I call it in the js it is not working , even if I used this.MyId() , I tried to debug but don't get any thing Jan 2, 2023 at 15:08
  • ok that simply means your getter is not called, how about using directly this.acc.data ? getSObjectValue(this.acc.data, IdAccount_FIELD) : '' in your recordId variable. Jan 2, 2023 at 15:11
  • @MohithShrivastava , I tried to use directly this.acc.data ? getSObjectValue(this.acc.data, IdAccount_FIELD) : '' in my recordId variable but don't work for me . Jan 2, 2023 at 15:16
  • add a debug on that navigate function and let me know what you see in the logs? Jan 2, 2023 at 15:18

1 Answer 1

0

Few issues I noted was your onclick calls a different method navigateToAccount and your JS method name is navigateToAccountPage.

But lets say we ignore this, I see your LWC class declaration missing extends NavigationMixin(LightningElement)

So your LWC should look like:

HTML:

<template>
    <div class="slds-box slds-theme_shade">
        <lightning-button label="Back to Account" onclick={navigateToAccountPage}> </lightning-button>
    </div>
</template>

JS:

/**
 * Created by Nagendra on 03-01-2023.
 */

import { LightningElement, api , wire, track } from 'lwc';
import IdAccount_FIELD from '@salesforce/schema/AccountMaster__c.Account__r.id';
import MyMethode from '@salesforce/apex/AccountRedirectPage.MyMethode';
import { NavigationMixin } from "lightning/navigation";
import {getSObjectValue} from "@salesforce/apex";

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

    @wire(MyMethode,{ objId: '$recordId'}) acc;

    get MyId() {
        return this.acc.data  ? getSObjectValue(this.acc.data, IdAccount_FIELD) : '';
    }

    navigateToAccountPage() {
        console.log(this.MyId)
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.MyId,
                objectApiName: 'Account',
                actionName: 'view'
            }
        });

    }
}

And it should work fine.

Also apart from that no need to mention like this Account__r.id. YOu can replace this with Account__c, both in apex and in LWC.

Documentation

5
  • i tried your proposition but did not worked for me , even the console.log did not appear Jan 3, 2023 at 13:45
  • I already executed this and its working fine. Where is your component located? Which page? Jan 3, 2023 at 13:48
  • my component i put it in record page of obj__c and i want through the button navigate to Account object which has master detail relation with obj__c Jan 3, 2023 at 14:16
  • Please check my html and JS, this works perfectly in a Record Page of AccountMaster__c object. Please check your method names. The onClick on your html is different that mine. Please verify if both the onClick method name is same as JS method name as navigateToAccountPage Jan 3, 2023 at 14:27
  • I verified all this but still don't work for me Jan 3, 2023 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .