0

My component is in the record page of MyObj__c . MyObj__c has a master detail with Account I tried to navigate from MyObj__c to the account assorted bu I always get an error

Error : {"status":500,"body":{"message":"Value provided is invalid for action parameter 'MyId2' of type 'Id'"},"headers":{},"ok":false,"statusText":"Server Error","errorType":"fetchResponse"}

=> Apex

 @AuraEnabled(cacheable=true)
    public static Id Class2(Id MyId2) {
        Id account = [SELECT Station__c FROM MyObj__c WHERE Id =:MyId2 LIMIT 1]Station__c;
                   return  account;
    }

=> HTML

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

=> JS

import { LightningElement, api , wire, track } from 'lwc';
import Class2 from '@salesforce/apex/MyClass.Class2';
import { NavigationMixin } from "lightning/navigation";

export default class RedirectToStation extends NavigationMixin (LightningElement) {
    @api recordId ;
    @track MyId2;
    navigateToAccountPage() {
        
        Class2({ MyId2: '$recordId' }).then(data => {

            if (data)
       {

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


       }
        
    }).catch(error => {

        console.log('Error : ' + JSON.stringify(error));
    })

}
}

1 Answer 1

0

There are a few mistakes in your code, please refer to the below code -

 @AuraEnabled(cacheable=true)
    //pass the recordId as a string
    public static Id Class2(String MyId2) {
        Id account = [SELECT Station__c FROM MyObj__c WHERE Id =:MyId2 LIMIT 1].Station__c;
        return  account;
    }
import { LightningElement, api, wire, track } from 'lwc';
import Class2 from '@salesforce/apex/MyClass.Class2';
import { NavigationMixin } from "lightning/navigation";
export default class RedirectToStation extends NavigationMixin(LightningElement) {
    @api recordId;
    navigateToAccountPage() {
        console.log('recordId', this.recordId);
        Class2({ MyId2: this.recordId }).then(data => {

            if (data) {
                // data is the recordId
                console.log('data', data);
                this[NavigationMixin.Navigate]({
                    type: 'standard__recordPage',
                    attributes: {
                        recordId: data,
                        //objectApiName: 'Account',
                        actionName: 'view'
                    }
                });


            }

        }).catch(error => {

            console.log('Error : ' + JSON.stringify(error));
        })

    }
}

You were passing the recordId in the wrong syntax. Removed the track variable MyId2.

1
  • thank you for your help , it worked for me Commented Jan 5, 2023 at 10:07

You must log in to answer this question.

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