Calling a web service with different arguments based on user input

  • Goal: I have a web service that can take either of two arguments: serial number and customer name.

You can have one of these arguments but not both.

  • Steps:

I created a dropdown called searchType.
I created a submit button called searchBtn
I created a resource called itemSearchApi
I know how to get the submit button to read the value from this dropdown.

The resource calls the API, but while the values passed through the query string are dynamic and based on what is inside a text box, the parameter names are not.

I can't figure out how to change the parameters. I want to do this:

  1. Select either serial number or customer name
  2. Enter a search term.
  3. If serial number is selected, send one particular query string to the resource.
  4. If customer name is selected, send a different query string to the resource.

e.g send **http://redactedstuff/?serial=WHATEVER or http://redactedstuff/?name=SOMETHING depending on the value of the select message/

I tried creating two different resources, but then I have trouble displaying the results into the application. Besides of which, creating a lot of Retool resources that connect to the same endpoint seems like it could get hard to maintain

I have heard that this can be accomplished by telling the searchBtn listener to run Javascript and then use the .trigger() function (itemSearchApi.trigger() ) but I cannot find any instructions on how to send arguments to the resource through this.

Hi @Michael_Stabosz,

Did you manage to solve this?

As it seems that user can either select serial number or customer number, then you can create a terniary in your url parameters, something like: {{ searchType.value === serial ? 'serial' : name }} can go in your Key and then {{ searchField.value }} in your value field, .e.g:

This will make both your value and parameters dynamic.

Hope this helps!

I abandoned this because I discovered the use case I was testing this for is not something we'll be doing. Thanks for the help though.

Okay so I later realized we do have to know how to do this. It only later occurred to me that I can create multiple Retool resources to trigger the same API call, but with different parameters. Then I can choose which resource to trigger by changing the event handler to "Run Script", and using an if-else statement based on what is selected.

var selectedItem = searchType.value;
if(selectedItem == "serialNo"){
    serialNoSearch.trigger();
} else if (selectedItem == "customerName"){
   customerNameSearch.trigger();
}

Pretty simple and I don't know why it took me so long to figure out I could just make 2 resources.