PauleneC
PauleneC

Reputation: 13

Is there a way to pass an array returned from a GET in a previous step to a Code Action step in Zapier?

Using the Zapier UI to set up a zap.


Updated information about the zap flow as requested:

  1. is a catch hook trigger that is fired when the user takes a specific action in the source app.
  2. is a GET API call to the app that gets the users subscriptions. For each user, an array of subscriptions is returned.
  3. is my problem step - I'd like to search the array to check if any of the subscriptions match a specific target string.
  4. conditional progress. Check the outcome of Step 3. If there was a match in the array, then take no more action, they are already subscribed. If they do not, then proceed.
  5. Send a POST to subscribe the user to the target subscription.

I have a GET that returns an array of objects, then I want to find if any of the objects have an ID that matches my target category ID string. If I choose the inputData.categoryId I can't get the entire array. I need to take action if the category ID is NOT in the array. Is there a way I can pass the entire payload of the GET into my next code action step?

I've tried passing in inputData.cateogryId but it runs the code step multiple times for each object in the array.

I'd like to be able to do something like this where inputData is the payload of the GET

const userRecords = JSON.parse(inputData);
output = {isNotSubscribed: false};
isNotSubscribed = userRecords.find(o => o.categoryId === 'string 1');

the input Data is in an array and looks like

[
  {
    "id": "string",
    "identifier": "string",
    "name": "string",
    "description": "string",
    "categoryId": "string",
    "contentId": "string",
    "signedDate": "2019-08-30T21:44:30.497Z",
  },
  {
    "id": "string",
    "identifier": "string",
    "name": "string",
    "description": "string",
    "categoryId": "string",
    "contentId": "string",
    "signedDate": "2019-08-30T21:44:30.497Z",
  },
  {
    "id": "string",
    "identifier": "string",
    "name": "string",
    "description": "string",
    "categoryId": "string",
    "contentId": "string",
    "signedDate": "2019-08-30T21:44:30.497Z",
  }
]

Upvotes: 1

Views: 591

Answers (2)

xavdid
xavdid

Reputation: 5262

David here, from the Zapier Platform team.

Rather than deal with the way Zapier serializes data between steps, I'd remove step 2 above and fold it into the JS code step. That way, the whole code would be:

// normally you'd need to wrap this in an `async` function, but Zapier does that for you
const res = await fetch('https://somesite.com/data');
const userRecords = await res.json();
return {isNotSubscribed: userRecords.find(o => o.categoryId === 'string 1')};

Upvotes: 1

Alexandre Le Bris
Alexandre Le Bris

Reputation: 11

Instead of using Zapier UI for your "GET", you should consider using Python code to do it. Postman can easily translate your request from the app to Python code.

If you do so, the output of your python "GET" will be an array of dic. It should looks like this :

url ="yoururl"
params= {"key":"value"}
payload = {"key":"value"}
headers = {"key":"value"}
response = requests.request("GET", url, data=payload, headers=headers)

Upvotes: 1

Related Questions