Is it possible to modify the response of a submission-created Function?

The author of the related topic is attempting to do the same thing I am, but I’m opening this thread to ask the question directly. It does seem possible to modify the response of a submission-created function, but it’s not documented anywhere that I have found yet. Details on my example.

The setup

  • A form (in inedx.html) with a single text field, handled with Netlify Forms
  • A Netlify function for the submission-created event in functions/submission-created.js

Expectations

When submitting the form, the expecation is that I will see the return value specified in submission-created.js

return {
  statusCode: 200,
  body: JSON.stringify({ message: "A new form submission came in" }),
};

Reality

When submitting the form, I see the standard Netlify Forms submission success screen

More info

The function is being invoked. I can confirm that with the function log:

The form submissions are coming in as expected. Can see those in the Verified submissions.

Something that makes this tricky

When running locally with netlify dev this works as expected. Submitting the form responds with the custom response set in submission-created. It makes sense because we don’t have the standard Netlify form success page locally, but does create confusion because it’s different than how things work in production.

If you want to show your own content after the POST rather than our generic thank-you page, make sure you include an action=/path parameter in your form definition.
Please read this below for more info! :slight_smile:

Thank you. Adding an action=/path lets you customize the HTML page that is returned by a submission-created Function, but you still can’t modify that HTTP response in the function. You’re still only able get a full HTML page from the response.

An example use case based on my demo repo above.

  • User submits the form with some value in a text input, let’s say “2”
  • Our submission-created function is triggered
  • The Function sees the submitted value, “2” and adds 2 so we have “4”

What I’d like to do is be able to return a custom response to them that says; “Thanks, your new number is 4”. I’d want to return that response from the handler like in my demo netlify-submission-created-func-demo/submission-created.js at main · tylergaw/netlify-submission-created-func-demo · GitHub

That isn’t possible by setting action=/path as far as I can tell.

I think there may be a little disconnect here, so let me take a step back and describe how our form handling and event-triggered functions work and then we can find a solution for your workflow :slight_smile:

When we get a form submission, we store the data, kick off an asynchronous spam check via akismet, and do whatever your form/site’s-statically-deployed-content says around interacting with the visitor’s browser.

Once we’ve received the results of the spam-check, if they are good (not-spam), we fire off the submission-received even-triggered function as well as any other form notifications you have.

So - if you read between the lines, the event triggered function both runs after the submission and likely after the user has navigated away from your site. So changing the response there won’t have any impact in for your visitor, only what your statically deployed content is coded to do, will happen. The event-triggered function also isn’t run AT ALL if the entry is marked as spam!

Now, can you do dynamic, real time things with functions? Yes! Just not that event-triggered function :slight_smile: I’d suggest that you could for instance POST to a function (not the event-triggered function; those are special and are not user-executable in production) - you’ll need to name it something different so it is a “regular function” not even triggered - but THAT function could:

  • do real-time calculation
  • present results to customer
  • present custom HTTP status codes etc to the customer.

Of course at that point you aren’t using our form handling feature, so your function would also have to store the results of the POST.

You could probably come up with a hybrid situation that both:

  • runs your custom function to do real-time calculation and return to visitor
  • also uses the automatic event-triggered function to do async writes to an external data source (or similar).

Hope that helps but let me know if not!

1 Like

where can we see a list of events that are triggered via form submissions? I had been using submission-created, but if submission-received is post-spam check that could be better.

Sorry, the list is what’s here - I rattled that off from memory (incorrectly), so this should be your reference:

I also might have been wrong about not running until the spam check is done but since we don’t notify you about those posts, I would be surprised if we run your function on them - let me know if you’re seeing that behavior, please!