Hi there, this is Erin Oz from the Zapier Support Team with another workflow idea to share!
Often a user wants to perform actions in their Zap only on certain items in an array.
The Filter by Zapier step does not filter each item in an array, so the solution here is to use a Code Step. You can use the filter method in Javascript to only allow the items you’d like to pass into the next steps of your Zap.
Example
This user has a Zap that triggers on a call ending. They would like to send a survey to all call participants that do not work at their company. What we’ll do here is use the filter method to only allow email addresses to continue in the Zap if they do not contain the @zapier.com domain.
For example, let’s pretend we have a trigger step that returns an array of five email addresses - three of them end with @zapier.com, so we’d like to filter those out of the actions that come next.
Our Code Step will accomplish three things:
-
All data that is brought into a Code Step is brought into the code as strings. In other words, even if the data being pulled in is already in an array, we will need to add code that turns the data into an array for the scope of the Code Step.
const emailsArray = inputData.emails.split(",");
-
The next line will filter out any email addresses containing ‘@zapier.com’.
const filteredEmails = emailsArray.filter(email => !email.includes('@zapier.com'));
-
The final line will output the array of filtered emails
output = :{filtered_emails: filteredEmails}]
The Input Data field is named emails
, which is important to making this code work as expected!
If you’d like to change this code to only allow emails that do contain ‘@zapier.com’, you can remove the “!” from !email.includes(‘@zapier.com)
.
You can also change the search string from ‘@zapier.com’
to anything you’d like to filter on.
We can see from our results here that we’ve removed the three email addresses that contained @zapier.com from the array, and only the emails from @client.com are returned in the output!
Does this make sense? Please let me know if you have any questions or need additional clarity on any of these steps!