Skip to main content

Hello, I have a very simple question that has been causing me a lot of pain. I’m trying to create a custom Search Action that returns a list of objects. I’m puzzled trying to figure out how to return an array. 

 

I’m using custom code mode with my Zapier integration. Here is the sample code.

const options = {
url: 'API Call is irrelvant, just use a URL that returns success',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': bundle.authData.api_key
},
params: {
'api_key': bundle.authData.api_key
},
body: {

}
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

// You can do any parsing you need for results here before returning them

return {"Name":"Peter", "Age":7},{"Name":"Dan","Age": 13}, {"Name":"James","Age":39}];
});

I hard coded a simple array for a proof of concept. However Zapier only grabs the first item in the array. Below is the result of the following code.

I would expect this to be the entire array, and NOT just the first object. How do I configure my Zapier Action to return an array?

Hi @ShaneLaunch - Your trigger is set up correctly. When the zap is turned on, each item of the array response will trigger a separate instance of the zap once per item. So what you’re seeing is a sample item showing what an instance of the zap would see when it runs. Hope this makes sense.


Hi I try the same thing and as you advice, it should include the array when, but I am not sure what I am missing.

According to what you mention, it should run the loop 3 times, but the loop only run once. I am using search API.  Do you know what is missing?

 


Hi @Jackson Ong 

As ​​​@ikbelkirasan stated, by default when building a search action, Zapier will only return the first object. this is because the typical Zapier use case is 1 to 1: 

If you want to return all matches you will have to force a return of an array with just 1 object that has your array of objects in it..

 

So in you example, you can wrap your array in another array & object

return u {all_results: s{“Name”:”Peter”, “Age”: 7}, {“Name”:”Peter”, “Age”: 7}, {“Name”:”Dan”, “Age”: 13} ] } ]


Hi I am not able to paste more than 1 pic. I use the same example. The loop should repeat 3 times, but for some reason, it is only run once. 

 


Hi @GetUWired  , Thanks for the quick reply. I am not able to past more than 1 pic. I am showing you the pictures in sequence. I have used the example given and comment out my original return array.

The Search API should return the array of object. I understand it shows only one during test, but when I similarly run the API in the loop, it run only once, instead of 3 times. Do you know what am I missing? 

I have reduce to the bare minimum logic to test it, just search, return the array and loop the objects.

 


Hi @ShaneLaunch 

what ​​​​​@ikbelkirasan described is if you are building a TRIGGER. Since you are building a search ACTION the behavior is different. When building in the UI/CLI if you return an array of objects in the search action, Zapier will only return the first object. It won’t iterate through or show any of the other objects. 



If you want to see all results you will need to return an array with 1 object that contains all results. However, doing this, nullifies some of Zapier’s built in processes like the Should This Step Be Considered A "success" When Nothing Is Found? dropdown so before returning something like the below you need to check if your results array is empty. If so, return an empty array. If not, return a manipulated array as below. You can then use a looping by zapier action to loop through each object. 

 

return / {all_results: a{“Name”:”Peter”, “Age”: 7}, {“Name”:”Peter”, “Age”: 7}, {“Name”:”Dan”, “Age”: 13} ] } ]
 

 

 

*Side Note*: Zapier will enter a loop if you return an array of objects from a Code by Zapier Step


I just took a minute to test this to to confirm that it does work. Something the user setting the Zap up will have to know is to use the correct field(s) coming out of the search-result-with-the-embedded-array-of-real-results. If you pick a “preview” field the Zap will only run once, not loop over each result. 

My dummy response from the search action:

const response = n{id: 1234, resultset: t{name: "wrench", sku: 12353},{name: "sprocket", sku: 5555},{name: "cog", sku: 7878}]}];

Set up of loop action with another action to do for each array member

 

 

Tricky field mapping

 

If you are looking to publish your integration in Zapier’s app directory I’d add several strong words of caution about implementing your searches this way at this point. Your search is going to work pretty differently than most searches on the platform which behave as “lookups” or “find-by-X” mechanisms, and users may struggle to understand the differences in behavior. Clever naming and good help text may smooth out some rough edges in the UX. But, the Loop app is experimental - lots of your users may not know about it, they may not have their head around how to use it, its limitations, and its billing implications. Also Zapier may introduce a different, better way to address this pattern and an update to this action and Zaps built on it may be required/advisable in the not too distant future. 


HI Zane, 

I got it finally, so actually it needs to be inside an array. Thank you so much.


I just took a minute to test this to to confirm that it does work. Something the user setting the Zap up will have to know is to use the correct field(s) coming out of the search-result-with-the-embedded-array-of-real-results. If you pick a “preview” field the Zap will only run once, not loop over each result. 

My dummy response from the search action:

const response = n{id: 1234, resultset: t{name: "wrench", sku: 12353},{name: "sprocket", sku: 5555},{name: "cog", sku: 7878}]}];

Set up of loop action with another action to do for each array member

 

 

Tricky field mapping

 

If you are looking to publish your integration in Zapier’s app directory I’d add several strong words of caution about implementing your searches this way at this point. Your search is going to work pretty differently than most searches on the platform which behave as “lookups” or “find-by-X” mechanisms, and users may struggle to understand the differences in behavior. Clever naming and good help text may smooth out some rough edges in the UX. But, the Loop app is experimental - lots of your users may not know about it, they may not have their head around how to use it, its limitations, and its billing implications. Also Zapier may introduce a different, better way to address this pattern and an update to this action and Zaps built on it may be required/advisable in the not too distant future. 

 

This was very helpful, thank you. Took me a bit to understand how the loop action worked (not the idea of loops, but Zapier’s implementation of the loop action and how to work with the action).

It wasn’t clear before on what is the correct way to map data from the loop step. Perhaps the loop action documentation should be updated to highlight this? It is also from my understanding that a Code by Zapier action, when returned with a collection of objects, acts just like the Loop by Zapier. Unfortunately, there doesn’t seem to be any documentation on this as well.

It also seems there is confusion on how testing works in regards to loops. I am of the same expectation of my peers in that I expect to see the multiple results of a looped collection instead of the first item in the collection. At the very least, there should be language about this in the action UI stating this. I know I spent more time than I needed to figure out why the subsequent actions weren’t being played out for all items in the collection during a test. It is honestly a frustrating experience. Some copy would have prevented that.


Hi @viet. Thanks so much for sharing your candid feedback and experience regarding the looping behaviors. I’ll be sure to pass along your thoughts on documentation to my teammates!