Help

Re: Using Scripting Block to push a webhook on button click

5881 2
cancel
Showing results for 
Search instead for 
Did you mean: 
dave_brand
6 - Interface Innovator
6 - Interface Innovator

Hey, is it possible to create some Action Buttons to push webhooks with json data with the scripting block?

39 Replies 39

Yes. Here’s an example that does this. It targets a web service in Google Cloud Platform, but could easily target any webhook endpoint as long as it is CORS-enabled.

//
// http post test (from GCP)
//
output.markdown('## HTTP POST Test (from GCP)');

let payload = {
    "field" : "name",
    "value" : "Bill Frenchy"
}

let postOptions = {
    method: "post",
    headers: {
        'Accept' : 'application/json',
    },
    body: JSON.stringify(payload)
}

const postResults = await fetch(gcpUrl, postOptions);
const jsonPost    = await postResults.json();

output.markdown("Display JSON Object");
output.inspect(jsonPost);
output.markdown(jsonPost.field + " = " + jsonPost.value);

Excuse my ignorance, but how is this script triggered?
Is there an “Action Button” functionality in Airtable that I’m not aware of?

Run button in Script Blocks.

:man_facepalming: of course… thanks!

Jonny_Matthews
4 - Data Explorer
4 - Data Explorer

Any idea how to alter this script to work with a Zapier or Integromat webhook? Been having a go but had no luck.

Trying to get all or a select few record fields included in the payload too…

Welcome to the community, @Jonny_Matthews! :grinning_face_with_big_eyes: I’ve got this working with an Integromat webhook, though I’m only passing the record ID and letting an Airtable “Retrieve a record” module in the scenario pull the rest of the data I need, but you could send over as much or as little as you want. Here’s what I’ve got, stripped down to just the basics:

let url = "https://hook.integromat.com/xxxxxxxxxxxxxxxxxxxxx?recordID=";
let table = base.getTable("Invoices");
let view = table.getView("Outstanding: Actions");

let record = await input.recordAsync("Pick a record", view);
await fetch(url + record.id);

When I click the button on an invoice record, it’s fed into the script, and the webhook is called with the ID included. Again, you can add more to the payload as you wish.

Chris_Ortega
4 - Data Explorer
4 - Data Explorer

@Justin_Barrett
how would I go about attaching field contents to that? I’ve tried a couple of different things to no avail

Without seeing precisely what you’re trying to do, I can suggest that …

  1. Field values needed by the webhook must be collected by the Script Block;
  2. Then passed to the webhook in some manner (either as URL parameters or as data).

#1 is achieved by adding to the script the necessary code to get the values of the record by the record ID. #2 is achieved by modifying the fetch() call with additional parameters on the URL variable.

Thanks for this Justin!

It works like a charm. When I press the button the blocks dashboard opens, is there any way to have this running in the background with seeing the blocks every time the script runs?