Best answer

Webhook doesn't run when being called from Code by Zapier

  • 21 April 2020
  • 8 replies
  • 2612 views

Hi there,

 

I’m using a Schedule by Zapier zap, that triggers every day, and uses Code by Zapier to fetch data from an Airtable using a Fetch request and then trigger another webhook to send a SMS message for each row.

 

When testing the Code by Zapier part, the webhook gets called and the SMS is being sent, but when running automatically through the scheduler, the API part gets the data but the other zap which is triggered by a webhook doesn’t fire.

 

Here’s the code:

var settings = {
  'method': 'GET',
  'headers': {
    "Authorization": "Bearer ...",
  },
};
fetch('https://api.airtable.com/...', settings)
.then(function(res) {
  return res.text();
})
.then(function(body) {  // body is the raw return from the API call
  body.toString();
    var jsonData = JSON.parse(body);
    var length = jsonData.length;
    var output = [];
      for (let i = 0; i < jsonData.records.length; i++){    // This for loop processes each record and adds it to the array
        output.push(jsonData.records[i].fields);
      }
      return body;
})
.then(function(hook) {
  var jsonData = JSON.parse(hook);
    var length = jsonData.length;
    var output = [];
      for (let i = 0; i < jsonData.records.length; i++){    // This for loop processes each record and sends to the SMS webhook
        let body = {
            mobile: jsonData.records[i].fields.Mobile,
            name:  jsonData.records[i].fields.Name
        };
        
        console.log(body);
        

//the following fetch part works when I’m testing the zap part (on Zapier) but not when it runs automatically when the scheduler calls it.


        fetch('https://hooks.zapier.com/...', {
            method: 'post',
            body:    JSON.stringify(body),
            headers: { 'Content-Type': 'application/json' }
        })
        .then(res => res.json())
        .then(json => console.log(json));
        
        output.push(jsonData.records[i].fields);
      }
  callback(null, output);
})
.catch(callback);

icon

Best answer by TimS 31 May 2020, 08:49

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

8 replies

Userlevel 7
Badge +12

Hi @Montag! I’m sorry, I’m not sure I’m following 100% here. 

 

It sounds like the part that’s not working (when the Zap is turned on) is the step that’s supposed to fetch the data from airtable is that right? When you look at the task history for the Zap, do you see any errors? Does it look like the code step is running successfully but nothing is happening (it’s not fetching anything)? Or is it fetching something but then the next step doesn’t run as it should? 

 

If you could share a bit more information about what you’re seeing when the Zap runs, that might help us to hone in on the issue - thanks!

Hi, thanks for the reply

here’s the flow:

Zap 1:

  1. Trigger every morning at 9
  2. Call Airtable API and fetch the data using Code by Zapier
  3. Call Zap 2 (webhook) with the data

Zap 2:

  1. Get the info
  2. Send SMS using Twilio

When I’m testing Zap 1 step 2, it’s working, Zap 2 gets called and I receive the SMS.

When the scheduler is triggering Zap 1, it doesn’t work.

 

I wrote the code of Zap 1 step 2 in the message above.

I guess something is not working regarding async, the function doesn’t wait for the inner fetch to be executed… but I’m not sure.

 

I have rewritten the code to make it more readable:

var settings = {
  'method': 'GET',
  'headers': {
    "Authorization": "Bearer xyz",
  },
};
fetch('https://api.airtable.com/xyz', settings)
.then(function(res) {
  return res.text();
})
.then(function(body) {  // body is the raw return from the API call
    body.toString();
    var jsonData = JSON.parse(body);
    var length = jsonData.length;
    var output = [];
      for (let i = 0; i < jsonData.records.length; i++){    // This for loop processes each record and adds it to the array
        output.push(jsonData.records[i].fields);
        abc(jsonData.records[i].fields);
      }
      //console.log(output);      
      callback(null, output);
      //return body;
})
.catch(callback);

function abc(obj) { // calling the webhook to send the SMS
  console.log(obj);
  fetch('https://hooks.zapier.com/hooks/catch/xyz', {
    method: 'post',
    body:    JSON.stringify(obj),
    headers: { 'Content-Type': 'application/json' }
  })
  .then(res => res.json())
  .then(json => console.log(json));
}

 

 

Thanks for trying to help :)

Userlevel 7
Badge +12

Thanks for the reply and for sharing the code that you’re using. It’s interesting that the code step and sending the webhook worked when you tested it but it isn’t working when the Zap is turned on. That suggests that there’s nothing wrong with the code itself, but something else is at play. 

 

I think that this might be something that the Support Team can help with - they can look and see if the Code step timed out and check if the webhook was sent. I’m going to escalate this post to the Support Team and I’ll be in touch via email shortly to ask for a couple more details that will help them with this. Thanks for your patience!

Userlevel 2
Badge +1

hey guys - did you ever figure this out? if yes would you share the remedy? I’m also interested in async and this port caught my eye

Userlevel 7
Badge +10

@bubba198  

I'm going to flag this post for the Zapier Community Team. They should have more insight on this and can escalate to the support team if needed.

Sit tight, and someone will contact you soon.

Userlevel 7
Badge +8

Hello @Montag - I have checked our ticket system and I see that you wrote back to us to let us know that you resolved this by re-writing your code. Would you be as kind to share it with us? Thanks so much!

Userlevel 7
Badge +8

Hey @bubba198 - I have written to Montag in hopes that they will respond to share the solution with us. Once we know, the solution will be shared here with everyone.

@AndrewJDavison_Luhhu Thanks for bringing this to our attention! 

Userlevel 7
Badge +3

Hi @bubba198 - I came across this post and wanted to show you a similar workflow with Airtable that I shared a couple days ago that might also help: https://community.zapier.com/tips-and-inspiration-5/check-spreadsheet-records-on-a-schedule-2587

While I’m not sure what the exact solution to @Montag’s code is, the way that I get around this is to run the actions in the same Zap multiple times - once for each record - in the same Zap.