Best answer

How do you include a variable-size array output field in the output data returned by a trigger?

  • 27 June 2020
  • 10 replies
  • 2131 views

Userlevel 1

For the Zapier App that I am developing, I am configuring a Trigger of type REST Hook. 

Say that my API returns details about soccer matches. The format of the data that gets posted to Zapier by my API would look something like this:

[{
"game_start_time": "2020-06-26",
"home_score": 2,
"away_score": 1,
"goals": [
{
"time": "45+2",
"player_name": "Lionel Messi",
"team": "home"
},
{
"time": "65",
"player_name":"Cristiano Ronaldo",
"team": "away"
},
{
"time": "90+5",
"player_name": "Lionel Messi",
"team": "home"
}
],
"attendance": 76000,
"penalties": [
{
"time": 21,
"player_name":"Cristiano Ronaldo",
"type": "yellow",
"reason": "diving"
}
]
}]

I want to be able to allow a user of my App to consume all the output fields available from my API, including `goals` and `penalties`. When I try to specify these output fields, they don’t seem to be available when I try to create a new Zap.

How do I add these fields?

icon

Best answer by jamesanselm 20 July 2020, 18:30

Hello @ForYourIT . I followed up with the Zapier developer support team through email. It is currently expected that if a Zapier Action is configured with an API endpoint that may return multiple results, then the Zapier Action will only return the first result.

 

For my example Zapier Action that attempted to retrieve the goals from a game (https://community.zapier.com/developer-discussion-13/how-do-you-include-a-variable-size-array-output-field-in-the-output-data-returned-by-a-trigger-3158?postid=12383#post12383), the output would be the object `games[0].goals[0]`.

View original

10 replies

Userlevel 1

To clarify, my use case isn’t for soccer games. I used it as an example.

Userlevel 7
Badge +8

Hi @jamesanselm - Thank you for your question! So that we can get you the right type of support, we kindly ask that you direct all developer support questions to our Support team directly: http://zpr.io/t4NNK  - This helps us get an accurate capture of your account details so that we may assist you quickly. Thank you for understanding!

Userlevel 7
Badge +9

@jamesanselm What are you doing to process the results in this trigger step? Are you on the CLI or the Developer Platform and if the Developer Platform are you using the code mode or the form mode? 

 

For example, here’s a screenshot of my trigger in code mode on the Developer Platform you’ll see I’m doing very little processing to the results, just grabbing a sub-object/array to return to the zap. 

Line 17, the only parsing I’m doing: const users = results.users

Are you doing any parsing like that?

Userlevel 1

I had posted my question on StackOverflow as well. https://stackoverflow.com/questions/62643662/zapier-rest-hook-trigger-how-do-you-include-a-variable-size-array-output-fie

Userlevel 1

@PaulKortman I am using the Developer Platform. I am using the code mode for the Perform function of the API Configuration for my Trigger with the trigger type REST Hook.

It used to look something like this:

return [bundle.cleanedRequest];

As a workaround to allow users of my Zap to use the goals and penalties however they want, I have modified the Perform function to look something like this:

data = bundle.cleanedResult;

data.goals_JSON = JSON.stringify(data.goals);
data.penalties_JSON = JSON.stringify(data.penalties);

return [data];

This way, a user of my Zap would then have to use a Code By Zapier step to reconstruct the goals or penalties array as they were returned by my API, using code such as:

let goals = JSON.parse(game_data.goals_JSON);

//format goals into a string in the format that the customer wants and add that string to the output for the Code By Zapier step.

 

Userlevel 7
Badge +9

@jamesanselm eww I don’t like having to force users into a code step! 

 

Could you change the trigger to just return the top-level information about the games (total minutes, final score, who played etc) and then have an action step that gets the goals for a specific game and another action step that gets the penalities for a specific game. 

 

This is far from ideal, but It would keep your users from having to do any code steps. We often have to do this when there’s a new invoice, follow up by getting the contact record and the product record to get information thats not available in the trigger step. 

Userlevel 1

@PaulKortman I am trying your approach of creating an Action step to retrieve all the goals, given a game_id. The problem I am noticing is that even though I am returning an array of multiple goals (as shown when I look at logs created using “z.console.log()”, Zapier seems to only return an object with the first goal.

My code for the API Endpoint looks something like this:

const options = {
url: process.env.SERVER_URL+'/game',
method: 'GET',
headers: {
'Authorization': bundle.authData.authentication_token
},
params: {
'game_id': bundle.inputData.game_id
}
}

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

const results = response.json;
if(results === null){
return [];
}

const games = results.games;
if(games === null || games.length <= 0) {
return [];
}

if(!("goals" in games[0])){
return [];
}

z.console.log("goals: "+JSON.stringify(games[0].goals));

return games[0].goals;
});

 

Userlevel 1

Hello @PaulKortman! Hope you had a nice long weekend. Would it be possible to take a look at my latest reply?

Userlevel 7
Badge +7

Hi @jamesanselm have you been able to solve this? Maybe can you clarify the current issue with an output example.

Userlevel 1

Hello @ForYourIT . I followed up with the Zapier developer support team through email. It is currently expected that if a Zapier Action is configured with an API endpoint that may return multiple results, then the Zapier Action will only return the first result.

 

For my example Zapier Action that attempted to retrieve the goals from a game (https://community.zapier.com/developer-discussion-13/how-do-you-include-a-variable-size-array-output-field-in-the-output-data-returned-by-a-trigger-3158?postid=12383#post12383), the output would be the object `games[0].goals[0]`.

Reply