0

Does anyone know if there's a way to programmatically submit a web to case form while also preventing the page redirect? I've embedded a web to case form in the html of a LWC. This LWC is added to a screen element of a flow. I'm using the renderedCallback() method within in the js file to auto-submit the form after the screen renders, but I'm having trouble with the page redirect after the submit event. Ideally, after the form is auto-submitted the flow finishes (this is the last element within the flow) and the user remains on the record they were viewing. However, currently the form is submitting, the flow is finishing, and then the entire page reloads and redirects the user back to our homepage.

Here's one of my attempts where I'm querying the form tag and then submitting it. I'm unable to use e.preventDefault() within this submit() event because while it does submit the form, the event instance is never actually triggered:

import { LightningElement } from "lwc";

export default class App extends LightningElement {
  renderedCallback() {
    
  const form = this.template.querySelector('form').submit(function (e) {
    e.preventDefault()
  })
}
}

Here's another attempt where I've created/dispatched a custom event. Again no luck with preventing the page redirect on submit():

import { LightningElement } from "lwc";

export default class App extends LightningElement {
  renderedCallback() {
    
  const form = this.template.querySelector('form');

  form.addEventListener('submit', function (e) {
    form.submit(function(evt) {
      evt.preventDefault()
    })
  })

  form.dispatchEvent(new CustomEvent('submit', {cancelable: true}));
  }
}

Does anyone know of anything else I could try? Or is there an alternative way to disable the redirect after submit()? I've seen some potential jQuery options but I'm trying to stick with vanilla js. Thanks.

2
  • You can override the submit action and then preventDefault from there : developer.salesforce.com/docs/component-library/bundle/…, check this topic Overriding Default Behaviors Commented Dec 14, 2020 at 14:22
  • Thanks Nagendra. Even with a custom override handler approach as explained in the doc, I'm unfortunately still having trouble triggering the submit() event so that the handler can listen for it.
    – mcg123
    Commented Dec 14, 2020 at 18:39

1 Answer 1

0

You can override the submit action and then preventDefault from there : https://developer.salesforce.com/docs/component-library/bundle/lightning-record-edit-form/documentation, check this topic Overriding Default Behaviors

You must log in to answer this question.

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