There are a couple of ways to build a dynamic URL in Mendix when you’ve got multiple search parameters. Let’s say $SearchCriteria is a object with fields like items, itemsCode, itemCodeCount, etc.
Option 1 is to build the URL in a microflow using multiple Change Variable activities.
First, create a variable called 'QueryString'
with an initial empty value (''
). Then add a series of Change Variable actions — one for each search parameter. Each activity will look something like this (adjusted per field):
If $SearchCriteria/ItemsCode != empty then $QueryString + (if $QueryString != '' then '&' else '') + 'itemsCode=' + $SearchCriteria/ItemsCode else ''
---------------
Option 2 is to use a Java or JavaScript action that returns the queryString
, which you can then append to your base URL. Below is code for javascript action:
let attributes = searchCriteria.getAttributes();
let queryParams = [];
// Loop through the attributes
attributes.forEach(key => {
let value = searchCriteria.get(key);
if (value && value.trim() !== "") {
queryParams.push(${encodeURIComponent(key)}=${encodeURIComponent(value.trim())}
); }
});
// Combine the query params
let queryString = queryParams.join("&");
return queryString;
Use a helper entity + loop + string concatenation in a microflow:
Create a helper entity (e.g., SearchParameter) with attributes:Name (string): the query parameter name (like itemsCode)Value (string): the value from the search box.
In your microflow, before calling the REST API:Create a list of SearchParameter objects — one for each search box with a non-empty value.For example, check each search box; if it has a value, create a SearchParameter with the parameter name and value and add it to the list.
Build the URL string dynamically:Initialize a string variable with the base URL, e.g. "https://apps./api/info?"Loop over the list of SearchParameter objectsFor each, append "Name=Value" to the URL string, prefixing with & except for the first oneThis way, only parameters with values get added, and the URL is correctly formattedUse the generated URL to call your REST API
Microflow Logic:
Initialize url = "https://apps./api/info?"
Initialize empty list of SearchParameter
For each search box:
If box.value is not empty:
Create SearchParameter with Name and Value
Add to list
For each SearchParameter in list:
If first parameter:
url += Name + "=" + Value
Else:
url += "&" + Name + "=" + Value
Call REST API with url