Ways to create a loop for multiple items in a Zap using a code step

  • 15 October 2019
  • 7 replies
  • 9316 views

Userlevel 7
Badge +11

Let's say I get a weekly email that has a table in it, the table has lots of information in the rows and I want to copy parts of that into a Google Sheet and Trello card.

Or I receive a multiple items from a webhook - a different number of items each time - and want to run a Zap action on each of the items in the hook.

Can anyone think of a way to do this using a code step rather than the old 'add everything to Google Sheets' workaround?



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

7 replies

Userlevel 7
Badge +9

I have a couple examples of ways I've done this in the past, but are you thinking in this hypothetical situation @Danvers that the data is coming in line-item format or plain text (single item). It matters a ton if Zapier recognizes it as line-item or is it things it's just one blob/string.


Userlevel 3

I've got a code step somewhere where I receive data in an array and loop through the array and call a webhook for each element in the array


built it a couple of years ago - before line item support



Userlevel 7
Badge +10

Any chance you could share @Bryan? sounds useful!


Userlevel 5
Badge

I wrote this and forgot to read your last bit saying to go around the Google Sheets workaround. You can do some element of this using a Code step, but honestly at that point I think it overcomplicates things. I'd push for this Google Sheets workaround if it's any simpler than what you were thinking:


I have a couple of Zaps where I do this using a Google Sheet. The basic example, is that you need to create a unique task/checklist/something for a variable number of things (clients/stores/departments/employees) per month/day/year.


For simplicity, I'm going to give an actual example I have created, but again, each piece of this is interchangeable based on your actual needs.


Actual Example: Multi-national corporation owns 3000 individual stores. Once a month, they need to run a checklist (in Process Street) for each store, for the store manager to complete a monthly audit.


To achieve this, I have a Google Sheet that contains the variable number of stores (they are adding and removing stores, and editing the contact info of store managers constantly). Let's call this tab of this sheet 'STORES'.


To automate the process, I have two zaps. The first is the scheduler zap that is triggered off of a Schedule by Zapier set to run on the first of the month. Inside of the Google Sheet, I have a second tab that has a 'JOIN' function, which joins all of the rows (stores from the 'STORES' tab) into a single value separated by commas (let's call this tab 'SEARCH').


I have a value like that for each value I want to pass into the checklists (Store Number, Manager Email, etc.). My scheduler zap then does a search and finds the row that contains each of those values (finds a dummy search value like 'PICKME' in column A).


Then, I use formatter to split the value into line items, splitting by comma. I repeat for each value I want to push into the checklist (Store Number, Manager Email, etc.). At the end of this first Zap, I have an action to create Spreadsheet Rows (with Line Item Support) in Google Sheets.


I put the output of the split to line items steps into each respective column as appropriate. This adds a row per store onto a third tab (let's call it 'RUN'). Since this runs once a month, that means that if I have 3000 stores, it will have 3000 rows at the beginning of month 1, 6000 rows at the beginning of month 2, and so-on, assuming no stores are added or removed in between.


So because there were a variable number of items in the 'STORES' tab, found in the 'SEARCH' tab, they were placed perfectly in the 'RUN' tab. This can kick off the perfect number of instances, which can vary from month to month without issue.


The second Zap is extremely basic. For the actual looping component of this, I have a second Zap, triggered off of 'New Spreadsheet Row' in that third Google Sheet tab ('RUN'). From there, all it has to do is take the info from the spreadsheet and then run the action for that one instance. In this case, that is to run a Process Street checklist.


Basically, I put the logic of the instances into another tool like Google Sheets, which can handle the number of loops via a JOIN function, and then keep a log of the instances while also triggering the appropriate number of runs. No coding required!


Userlevel 7
Badge +11

@BlakeBailey - don't get me wrong, I like the Google Sheets option (with at least two Zaps then triggering off new rows), I think it's a great no code option for folks (like myself) who aren't super skilled in that area. I thought it would be interesting to ask for alternatives as it's great to expand your options 🙂

@PaulKortman This is a hypothetical, but I'm assuming that the input we have is line items rather than a blob of text. That way it could apply to a simple Webhook by Zapier trigger, or purchased items from an ecommerce app, etc.

@Bryan I second @Andrew_Luhhu's comment - if you're able to share that code step, that would be great!


Userlevel 7
Badge +9

I don't have a code block for this but perhaps a theory code block using storage... since the data is in line item format it will call the code step once for each row passing along that row's data to the code block. I'd then test for a certain value like `if(inputData.ColumnD = "true") {do stuff}` and the stuff I would do is set a storage value so the full code block would be

const store = StoreClient('your secret here');

if(inputData.ColumnD = "true") {

const value = await store.get('TrueRows');

const newValue = value + "| " + input.Data.ColumnD;

await store.set('TrueRows', newValue);

};

return {}; // return nothing, because your next step would be a storage get value,

// and then split on the pipe character | to get the line items again

If it were running the rows/content as a blob or string then you could convert it to an array within the code and run a for loop on it. That is what I typically do, the above is theory code, test and use at your own risk.


Userlevel 7
Badge

If any of you are members of Airtable Community this post has code steps for Zapier Loops:

https://community.airtable.com/t/zapier-loop-for-each-record-in-view/8044