3

I'm working on a requirement where I want a button which will open a modal to create a new Task record in LWC. For this, I tried using NavigationMixin. But onclick of Save in the modal, it redirects to the newly created record page instead of closing the modal. Is there any way to skip the navigation to the newly created record and close the modal only.

Below is the code of NavigationMixin that I'm using:

this[NavigationMixin.Navigate]({
    type: 'standard__objectPage',
    attributes: {
        objectApiName: 'Task',
        actionName: 'new'
    }
});

Thanks in advance.

6
  • Is it a quick action? Just to be sure Commented Nov 11, 2019 at 10:59
  • @ChristianCroce, No. It's not a quick action. We have an LWC component, where we want a button to create a new task. Commented Nov 11, 2019 at 11:33
  • 1
    OK, i think that's the standard way of the action. if you want to avoid it, you need to create your own component Commented Nov 11, 2019 at 11:41
  • Did you ever find a solution for this?
    – zaitsman
    Commented Jun 10, 2020 at 7:25
  • 1
    @zaitsman, No. I didn't find any standard solution for this. We have customized screens for Tasks. Commented Jun 12, 2020 at 9:29

2 Answers 2

9
this[NavigationMixin.Navigate]({
  type: 'standard__objectPage',
  attributes: {
      objectApiName: this.childObjectApiName,
      actionName: 'new'                
  },
  state : {
      count: '1',
      nooverride: '1',
      useRecordTypeCheck : '1',
      defaultFieldValues: inheritParentString,
      navigationLocation: 'RELATED_LIST'
  }
});

You can append navigationLocation: 'RELATED_LIST' to state, and it will prevent navigation away from the current page. For some reason, this is not in the documentation and took me hours of crying into my palms before I found it in the url when using the action from a standard related list.

2
  • 1
    it works perfectly.
    – vkm
    Commented Mar 14, 2022 at 9:36
  • It throws error if I add defaultFieldValues: inheritParentString, but after removing it works perfectly, tx Commented Feb 3, 2023 at 14:56
0

You can use aura component for achieving this. First, you have to send the call to aura component from your lwc component, use this link - https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.events_sending_to_aura_components

In lightning aura component you have to simply use this code in handler onclick action method for achieving the scenario :

var createRecordEvent = $A.get("e.force:createRecord");
    createRecordEvent.setParams({
        "entityApiName": "Task",
        "defaultFieldValues": {
            'WhatId':filters
    },
        "navigationLocation": "LOOKUP",
    "panelOnDestroyCallback": function(event) {
        $A.get("e.force:closeQuickAction").fire();
        // window.location.href = window.history.back();
}
    });
    createRecordEvent.fire();
    }

Hope this helps

You must log in to answer this question.

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