Skip to main content

Hi ,  I am attempting to navigate to the contact record page upon button click. In the button click handler method, I invoke an Apex method to retrieve the contact ID, which is then utilized in the NavigationMixin.Navigate to direct to the contact record page. However it is not working inside the promise  but working outside the promise. can you please help me is there any way to make it work or alternative solution.    my code is something like below    handlebtnclick(event){  getcontactid({fullname: this.customerdata.FullName}).

        then((result) => {

            console.log(result);

            console.log('Navigate:',this[NavigationMixin.Navigate]);

            this[NavigationMixin.Navigate]({

                type: 'standard__recordPage',

                attributes: {

                    recordId: result,

                    objectApiName: 'Contact',

                    actionName: 'view'

                }

            });

        })

        .catch(error => {

            console.log(error);

        })  

]  

 

@* Salesforce Developers * 

1 个回答
  1. 2025年9月16日 07:50

    Hi

     

    1. Your component probably

    did not extend NavigationMixin properly.

    2. this inside .then() needs to be correctly bound (you are using arrow function 

    3. NavigationMixin expects the component to be properly initialized when calling navigate. 

     

    Please follow this documentation link:-

    https://developer.salesforce.com/docs/platform/lwc/guide/use-navigate-page-types.html

     

    import { LightningElement } from 'lwc'; 

    import { NavigationMixin } from 'lightning/navigation'; 

    import your class here  

     

    export default class YourComponent extends NavigationMixin(LightningElement) { 

        handlebtnclick(event) { 

        getcontactid({ fullname: this.customerdata.FullName }) 

            .then((result) => { 

                if (result) { 

                    this[NavigationMixin.Navigate]({ 

                        type: 'standard__recordPage', 

                        attributes: { 

                            recordId: result, 

                            objectApiName: 'Contact', 

                            actionName: 'view' 

                        } 

                    }); 

                } else { 

                    console.error('No Contact ID returned'); 

                } 

            }) 

            .catch(error => { 

                console.error(error); 

            }); 

    if this resolve you query , Please mark as helpful. 

    Thanks. 

0/9000