Function working locally, not when deployed

Thanks @jen Good article! A lesson in imperative vs declarative, perhaps :sweat_smile:

I was just about to reply with the post below (which I’m going to leave in case it helps anyone out), and it occurred to me that I never tested it deployed to production. I had only tested it working off the build preview URLs because I didn’t want the site live yet.

Long story short, I temporarily deployed it live and the function fires. It would seem event-based functions are not set up to fire from Preview Deploys :confounded:

I’m sure I didn’t miss this footnote in the docs somewhere. Though I could be wrong. Please link me if anyone sees it. Otherwise this should probably be clarified there or, if it’s a bug, thrown in the backlog :sweat_smile:

Thanks again @jen - legend - I had given up on this and wouldn’t have tried again had you not replied!


Post below for historical purposes
The submission-created event function (only one I tested) only fires on a live site. It is not working on preview URLs.

I’ve adjusted the function but still something is not right when deployed :confused:

Test 1/2

  1. Tested the refactored function locally with netlify dev and netlify functions:invoke submission-created:
    • Forms visible on the Netlify Admin dashboard are deleted :white_check_mark:
  2. Submitting a form deployed on Netlify:
    • Forms visible on the Netlify Admin dashboard are not deleted :x:

Build Log Extract

Just so we see the netlify dependency gets installed and the function is picked up.

logs

Test 2/2

  1. I duplicated the function (duplicate.js) so that I can call it manually when deployed (read that you can’t call deployed event-based functions manually - you get 403’d)
  2. Called it in the browser - Get our console log from the function and forms visible on the Netlify Admin dashboard are deleted :white_check_mark:

I am at a loss :sweat_smile:

submission-created.js

const NetlifyAPI = require('netlify')

exports.handler = async function (event, context) {
  const client = new NetlifyAPI(process.env.NETLIFY_API_ACCESS_TOKEN)

  const submissions = await client
    .listSiteSubmissions({
      site_id: process.env.SITE_ID
    })
    .catch((e) => console.log('Error getting submissions', e))

  if (submissions.length) {
    for (i = 0; i < submissions.length; i++) {
      const submissionID = submissions[i].id
      console.log('starting to delete submission: ', submissionID)
      await client.deleteSubmission({ submission_id: submissionID })
      console.log('deleted submission: ', submissionID)
    }
    return {
      statusCode: 200,
      body: 'Submissions deleted',
    }
  } else {
    console.log('No submissions to delete')
    return {
      statusCode: 200,
      body: 'No submissions to delete',
    }
  }
}