Best answer

Looping dynamic fields in search action


Userlevel 1

In search action, I have enabled dynamic field which populates input fields based on an API call.

Now I want to loop through this input fields.

While searching I want to pass the entered search fields alone. How I can achieve this in Zapier?

 

Dynamic fields generates input fields 

 

 

Generated input fields for search

 

Now I want to fetch fields where value is entered and pass to the corresponding API to fetch search results.

How to achieve this.

 

icon

Best answer by Zane 24 May 2022, 21:11

View original

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

5 replies

Userlevel 1

Thanks Zane. This approach helped in resolving the issue.

Userlevel 7
Badge +9

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

 

used like

 

const options = {
  url: 'https://example.ngrok.io/message',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-API-KEY': bundle.authData.api_key
  },
  params: {
    'api_key': bundle.authData.api_key
  },
  body: {
    ...bundle.inputData
  }
}

 

If you need to reference some properties by known names as well as dynamic ones you can use destructuring assignment. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

 

Lots of options… another available option is Object.keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys which would get you an array of the keys in inputData

 

 

Userlevel 1

Adding more details here

I need to fetch input field name and its value entered. 

This is required for search API call where in all search fields should be passed as request param with name value pair. 

My call would be like searchEndPoint? firstName=Jane&age=30&location=london

I need to fetch those to make above call.

Userlevel 1

Thanks Jane for quick response. I am using code mode to handle the action.

since input field names can be anything based on the above field. 

If I refer with static name like bundle.inputData.firstName, its working fine.

How I can loop through all the input fields like 

bundle.inputData.firstName

bundle.inputData.age

bundle.inputData.location

 

Catch here is number of input fields are dynamic and names of input fields are dynamic.

can you suggest a viable option here.

Userlevel 7
Badge +9

All input is available in your perform function in bundle.inputData. Try using Javascript spread syntax to dynamically reference all the fields, like, ...bundle.inputData

Switch over to “code mode” for more control over the request if you’re not already.