function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Priyanka Reddy 74Priyanka Reddy 74 

How to call Lightning Navigation function() from another function.

getRequest(){
        Request({recordId : this.recordId})
        .then(data => {
            alert('1');
            this.newRecordId = data;
        })
        .catch(error=>{
            alert(error)
        })
            this.navigateNext();
}
 
navigateNext() {
        alert('2');
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.newRecordId,
                actionName: 'view'
            }
        });
    }


Hi All,

I want to call navigateNext() from getRequest() in LWC, In some cases, the page is navigating some times it is not.

I put 2 alert statements to understand flow For the first time when i call the getRequest() for the first time alert('2') which is in navigatinNext() and then alert('1')  is getting printed, from the second time output is as alert('1') then alert('2');

Can someone help me where I went wrong?
 

Maharajan CMaharajan C
Hi Priyanka,

Put your navigateNext method inside the Request promise solve block. Because apex imperative method is a one type of Asynchronous JS so it won't wait for the request call result instead it will execute the next line . so you have to call the  navigateNext method inside the Request data block.
 
getRequest(){
        Request({recordId : this.recordId})
        .then(data => {
            alert('1');
            this.newRecordId = data;
            this.navigateNext();
        })
        .catch(error=>{
            alert(error)
        })
}


Thanks,
Maharajan.C

 
Malika Pathak 9Malika Pathak 9
 Hii Priyanka Reddy,
 In your code you call navigate next function after the request is completed that does not matter what they get error or recordId in case of recordId maybe your code run but if the recordId not get or provide some kind of error also fire navigation which causes you a problem.
 you check this code for your program this you have to declare the recordId variable 
 
getRequest()
  {
        Request({recordId : this.recordId})
        .then(data => {
            alert('Inside get Request ');
            this.newRecordId = data;
            this.navigateNext();
        })
        .catch(error=>{
            alert(error)
        })
  }

  navigateNext()
  {
        alert('Inside Funtion 1');
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.newRecordId,
                actionName: 'view'
            }
        });
    }

if you find this helpful mark it as the best answer.
  thanks 
  Malika Pathak.