Skip to main content

Make.com/Airtable webhook


Forum|alt.badge.img+3

Hello,

I am looking for some help on an automation I am trying to run.  I am trying to use a make.com webhook with airtable so that when a new record shows up in airtable it sends the webhook to make.com.  I have the script that I am using and the connection and everything works but the webhook does not send the correct bundle of information.  It sends the record ID and then just generic information like 'header 'recipient', etc.  I am a school teacher and not a coder so I have been trying to get Chatgpt to help me with the script and I feel I am really close but don't know the scripting language enough to diagnose the problem.  Any help would be appreciated.

let table = base.getTable("Injury Report Form");
let recordId = input.config().recordId;

// Fetch the specific record
let record = await table.selectRecordAsync(recordId);

// Check if the record is fetched properly
if (record) {
    // Prepare the data to be sent to the webhook
    let data = {
        "recordId": record.id,
        "fields": {}
    };

    // Iterate through each field and add to the data object
    table.fields.forEach(field => {
        data.fields[field.name] = record.getCellValue(field.name);
    });

    // Webhook URL
    let webhookUrl = "https://hook.us1.make.com/mywebhook";

    // Send POST request
    await fetch(webhookUrl, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });
}
 
Thank you Mike

16 replies

TheTimeSavingCo
Forum|alt.badge.img+28

Hmm, I tried running your code and this was the webhook result, which looks good! 


Could you try logging your "data" object to see what's in it before it's sent?  i.e.

table.fields.forEach(field => { data.fields[field.name] = record.getCellValue(field.name); }); console.log(data)

 


Forum|alt.badge.img+2

Easiest way is after the webhook sends over the record ID you probably need to use the "Get Record" module to fetch the rest of the data for that record.

But I think you need to fill in the "fields": {} array with the field names of the data you want to retrieve.


ScottWorld
Forum|alt.badge.img+33
  • Brainy
  • 8778 replies
  • August 10, 2024

@Schmidt127 

As @Collabability mentioned above, you don’t need that long script.

You just need a short & simple script that only sends the Record ID to Make’s webhooks.

I have written up full instructions on how to do this at this link.

– ScottWorld, Expert Airtable Consultant


ScottWorld
Forum|alt.badge.img+33
  • Brainy
  • 8778 replies
  • August 10, 2024

Also, in addition to what I wrote above, remember that you can’t use the trigger “when a new record is created” unless your data is coming from OUTSIDE an Airtable data entry screen  — such as coming from a form or coming from an API integration. You said in your original post “when a new record shows up in airtable”, so I’m assuming that your records are coming in from somewhere else, which would work fine. 

— ScottWorld, Expert Airtable Consultant


TheTimeSavingCo
Forum|alt.badge.img+28
Collabability wrote:

Easiest way is after the webhook sends over the record ID you probably need to use the "Get Record" module to fetch the rest of the data for that record.

But I think you need to fill in the "fields": {} array with the field names of the data you want to retrieve.


@Collabability 
Yeap, `fields` is automatically populated here by the script when I ran it, and sends the cell values over to Make.  Did it work differently for you? 

Using a "Get record" module with the record ID definitely works, but we’d then need to keep an eye on any changes to field names in Airtable and update them in Make as necessary.  One workaround is to use the "Use column ID" setting to avoid issues, but in my experience subsequent modules end up using the IDs instead of the names which makes things hard to maintain as it just shows "fldUCR1V4ItIQAGm6" or whatever instead of "Name"

Since the script is already working, I feel like it'd be easier to just investigate what’s happening with the current workflow, you know what I mean?  That way all the data flows into Make via the webhook and we can just grab it from there without having to maintain or use an additional module / op

---
@Schmidt127if you end up wanting to just use the record ID instead, you don't need to make any changes to your workflow or script.  You're already sending the record ID over to Make as it is

If you'd really like to clean it up, you could just get rid of this bit:

 

// Iterate through each field and add to the data object table.fields.forEach(field => { data.fields[field.name] = record.getCellValue(field.name); });

 

And the line `"fields:{}"` here:

 

let data = { "recordId": record.id, "fields": {} };

 

Those are the lines that help you consolidate your record's data to push into Make, and so once you remove them you'll only be sending the record ID over, and you'd use that record ID in a "Get Record" module


ScottWorld
Forum|alt.badge.img+33
  • Brainy
  • 8778 replies
  • August 11, 2024

@TheTimeSavingCo Your comment about the field name changes makes no sense, because the exact same problem would happen if you use a script.

Make’s “Get A Record” module will always return the most up-to-date field names. If future modules refer to an outdated field name, then they won’t work properly.

The exact same thing would happen with a script. if you send the most recent field names from a script to Make, but future modules are referring to the old field names, then they won’t work properly.

There is no advantage to using the script.

The easiest way to do all of this is to follow my technique of just sending the Record ID to Make, and have it pull in the current information.

Also, most importantly, the rule of thumb is to not change field names that are used in 3rd-party apps, because of dependency issues like this.


TheTimeSavingCo
Forum|alt.badge.img+28
ScottWorld wrote:

@TheTimeSavingCo Your comment about the field name changes makes no sense, because the exact same problem would happen if you use a script.

Make’s “Get A Record” module will always return the most up-to-date field names. If future modules refer to an outdated field name, then they won’t work properly.

The exact same thing would happen with a script. if you send the most recent field names from a script to Make, but future modules are referring to the old field names, then they won’t work properly.

There is no advantage to using the script.

The easiest way to do all of this is to follow my technique of just sending the Record ID to Make, and have it pull in the current information.

Also, most importantly, the rule of thumb is to not change field names that are used in 3rd-party apps, because of dependency issues like this.


@ScottWorld.  Interesting!  What's the advantage of having the additonal "Get a record" module instead of pushing it all in if they're both facing the field names issue?  Bearing in mind that the script already works and is pushing in all the data, and we'd have to set up a new "Get a record" module and use an additional op per run here


ScottWorld
Forum|alt.badge.img+33
  • Brainy
  • 8778 replies
  • August 11, 2024

It’s always fine to use any technique that works correctly, but simplicity & less coding is always the path that I like to follow.

When you just send the Record ID, the script is significantly simpler and you can manage everything in a no-code way over at Make.

Also, I’m not sure how the more complex script handles different field types like simple arrays and complex arrays, but Make already recognizes the various Airtable field types without needing to account for them in the script.

As long as a method works, it should be fine, but I always steer for simplicity and no-code. 


TheTimeSavingCo
Forum|alt.badge.img+28
ScottWorld wrote:

It’s always fine to use any technique that works correctly, but simplicity & less coding is always the path that I like to follow.

When you just send the Record ID, the script is significantly simpler and you can manage everything in a no-code way over at Make.

Also, I’m not sure how the more complex script handles different field types like simple arrays and complex arrays, but Make already recognizes the various Airtable field types without needing to account for them in the script.

As long as a method works, it should be fine, but I always steer for simplicity and no-code. 


Thanks for clarifying your approach!  Simplicity is always a solid choice.  We might just have different perspectives on simplicity here as not needing to create a new module seems simpler to me in this situation!

 

Forum|alt.badge.img+3
  • Author
  • Participating Frequently
  • 5 replies
  • August 11, 2024
TheTimeSavingCo wrote:

Hmm, I tried running your code and this was the webhook result, which looks good! 


Could you try logging your "data" object to see what's in it before it's sent?  i.e.

table.fields.forEach(field => { data.fields[field.name] = record.getCellValue(field.name); }); console.log(data)

 


Thank you for responding.  I don't know exactly as to what you are describing, but I am trying some  of those things.  I know in your screenshot that my bundle did not include the recordID: recSjS...........that was missing.  I am still having trouble getting the webhook to come from airtable and start my automation.  I I have 

1. Webhook

2. Webhook Response

3. Google Docs

4.  The rest of my automation........

5. I think my automation is going to be pretty close to done, but I j ust can't get it to work correctly.

Thank you so much.

Mike


Forum|alt.badge.img+3
  • Author
  • Participating Frequently
  • 5 replies
  • August 11, 2024
Collabability wrote:

Easiest way is after the webhook sends over the record ID you probably need to use the "Get Record" module to fetch the rest of the data for that record.

But I think you need to fill in the "fields": {} array with the field names of the data you want to retrieve.


The script i am using now, does bring over all of the fields.  So manually i can get those fields over there when i trigger my automation manually, but I still just can't get the webhook to work.  Thank you.

Mike


Forum|alt.badge.img+3
  • Author
  • Participating Frequently
  • 5 replies
  • August 11, 2024
ScottWorld wrote:

@Schmidt127 

As @Collabability mentioned above, you don’t need that long script.

You just need a short & simple script that only sends the Record ID to Make’s webhooks.

I have written up full instructions on how to do this at this link.

– ScottWorld, Expert Airtable Consultant


Thank you, I got a script from the internet and believe it works for the most part.

const {recordId} = input.config(); // Ensure you have an input variable configured with the name `recordId` and the value "Airtable record ID"
const tableName = "Injury Report Form";

async function sendAllFieldsToWebhook(webhookUrl, recordId, tableName) {
    // Retrieve the table using the specified name
    const table = base.getTable(tableName);

    // Retrieve all fields from the record
    const fieldNames = table.fields.map(fieldObj => fieldObj.name);
    const record = await table.selectRecordAsync(recordId);
    if (!record) {
        throw new Error("Record not found. Should never happen.");
    }

    // Construct the body of the request with field values
    const body = {};
    fieldNames.forEach(name => body[name] = record.getCellValue(name));
    const options = {
        method: "POST",
        body: JSON.stringify(body),
        headers: {"Content-Type": "application/json"}
    };

    // Send the POST request to the webhook URL
    const response = await fetch(webhookUrl, options);
    console.log(response);
}

async function main() {
    await sendAllFieldsToWebhook(webhookUrl, recordId, tableName);
}

await main();
 
Again, I am a teacher of a high school and just got into make a couple of weeks ago and so don't understand some of the things you guys are saying, but I go back to youtube, etc, chatgpt, and try to figure it out.

Forum|alt.badge.img+3
  • Author
  • Participating Frequently
  • 5 replies
  • August 11, 2024
ScottWorld wrote:

Also, in addition to what I wrote above, remember that you can’t use the trigger “when a new record is created” unless your data is coming from OUTSIDE an Airtable data entry screen  — such as coming from a form or coming from an API integration. You said in your original post “when a new record shows up in airtable”, so I’m assuming that your records are coming in from somewhere else, which would work fine. 

— ScottWorld, Expert Airtable Consultant


I did not know that.  I was copy and pasting my new records from previous records.  That was not the underlying problem, but that is good to know.  So when you have a form of 30 fields, what methods do you use to not have to fill in the form over and over again to test?

 


Forum|alt.badge.img+3
  • Author
  • Participating Frequently
  • 5 replies
  • August 11, 2024
TheTimeSavingCo wrote:

@Collabability 
Yeap, `fields` is automatically populated here by the script when I ran it, and sends the cell values over to Make.  Did it work differently for you? 

Using a "Get record" module with the record ID definitely works, but we’d then need to keep an eye on any changes to field names in Airtable and update them in Make as necessary.  One workaround is to use the "Use column ID" setting to avoid issues, but in my experience subsequent modules end up using the IDs instead of the names which makes things hard to maintain as it just shows "fldUCR1V4ItIQAGm6" or whatever instead of "Name"

Since the script is already working, I feel like it'd be easier to just investigate what’s happening with the current workflow, you know what I mean?  That way all the data flows into Make via the webhook and we can just grab it from there without having to maintain or use an additional module / op

---
@Schmidt127if you end up wanting to just use the record ID instead, you don't need to make any changes to your workflow or script.  You're already sending the record ID over to Make as it is

If you'd really like to clean it up, you could just get rid of this bit:

 

// Iterate through each field and add to the data object table.fields.forEach(field => { data.fields[field.name] = record.getCellValue(field.name); });

 

And the line `"fields:{}"` here:

 

let data = { "recordId": record.id, "fields": {} };

 

Those are the lines that help you consolidate your record's data to push into Make, and so once you remove them you'll only be sending the record ID over, and you'd use that record ID in a "Get Record" module


Yes, no data, and the recrodID did not show up?


TheTimeSavingCo
Forum|alt.badge.img+28
Schmidt127 wrote:

Yes, no data, and the recrodID did not show up?


If you could provide a read-only invite link to a duplicated copy of your base with some example data I could take a look at this for you! https://support.airtable.com/docs/adding-a-base-collaborator#adding-a-base-collaborator-in-airtable


dilipborad
Forum|alt.badge.img+20
  • Brainy
  • 215 replies
  • August 12, 2024

Hello @Schmidt127 
Note → @ScottWorld & @TheTimeSavingCo have already explained most of the things about how to send and handle those webhooks. So I don't want to repeat it. But just try to explain to you the nature of airtable automation.

Before you go to the part of the call make a webhook. These are things you need to know.
First of all, just understand your trigger point is to add a record as you mentioned and @ScottWorld confirmed about that in earlier responses. But for more information, there are multiple ways to add records 1) Manual 2) Via Form submission 3) Via API  4) Import data.

1) Manual → When you add a record as a manual(in Airtable) by default all the fields are blank at the starting point just recId(rec****) is there to access in automation.

2) Via Form → When an Airtable form is submitted it also creates a record on a specific table and it comes with all the data(fields) which is set on the form.

3) Via API → It is the same as form but instead of using the form you can use any third-party tool or directly send using their API Airtable Web API 

4) Import → Just create multiple records based on your source of data.

Now the important thing is using any of the approaches by default whenever the record is created its have recId(rec****).

But,
Here is a thing you need to understand that about automation nature.
No matter which approach you've used when you set trigger automation when the record is added. Then automation is triggered instantly when the record is created. Now when you use the 1) method that time is record is created but all other visible fields do not have data(Maybe you're just filling it, But automation already triggered on add). So in this case you can't pass the other data fields because it is not there to pass alongside your hook.
But this same thing works with all other methods because there is data so you can pass it alongside your request.
If this is the case for you then use condition match when the record is entered to view the approach for trigger automation.
It's a bit long but I hope this helps.
👍


Reply