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
AndegosuAndegosu 

LWC: Chain javascript method after NavigationMixin

I have a datatable with a new button that uses the NavigationMixin.Navigate functionality in LWC.
import { NavigationMixin, CurrentPageReference } from 'lightning/navigation';



After the record is created it default navigates to the record, I had to add "navigationLocation: 'RELATED_LIST'" to the state to make it stay on the original record page where the component is placed.
However, after the dialoge is done and new record has been created, it stops running. I want to refresh the datatable to display the new record but I can't run any code after NavigationMixin. 

Is there anyway to solve this?

Code run when clicking new:
createNew() {
    let defaultFieldValues = '';
    this.loading = true;
    if(this.parentFieldName !== null) {
      defaultFieldValues = this.parentFieldName +"="+this.recordId;
    }
  
    this[NavigationMixin.Navigate]({
      type: 'standard__objectPage',
      attributes: {
        objectApiName: this.objectName,
        actionName: 'new'                
      },
      state : { 
          nooverride: '1',
          defaultFieldValues: defaultFieldValues,
          navigationLocation: 'RELATED_LIST',
          recordTypeId: this.recordTypeId
      }
    }).then(result => {
      console.log(result);
      return refreshApex(this.wiredResult);

    }).catch(error => {
      console.log(error);
    }); 
  }

 
David Zhu 🔥David Zhu 🔥
NavigationMixin.Navigate  is a component calls API to navigate to another page in the application. It won't return a Promise.
So line 20: ".then(result => {" is invalid.

I would suggest using a modal window containing a lightning-record-edit-form to create the record, then use onsuccess event to call refreshApex.
You may refer this URL for how to implement lighting-record-edit-form.

https://developer.salesforce.com/docs/component-library/bundle/lightning-record-edit-form/documentation
 
AndegosuAndegosu
I was afraid I would have to use that, which is a real shame. Since the datatable should be dynamic I would need to dynamically render record edit forms based on the relevant page layout which imagine would be a lot fo work.
Brandon ToledoBrandon Toledo
Hi, Did you find a solution to this ? I'm trying to do something similar