But I get this error: TypeError: Cannot read property 'jquery' of undefined. I want the final URL to be formatted like: https://xyz.api.service.com/v2/task?method=create&title=Sample%20Task&priority=HIGH&token=5678. What’s the correct way to convert action fields into query parameters without using jQuery?
zapier’s scripting environment doesn’t include jquery, so $.param() throws that error. use urlsearchparams instead - way cleaner than building strings manually:
var params = new urlsearchparams(bundle.action_fields);
params.append('token', bundle.request.params.token);
bundle.request.url = "https://xyz.api.service.com/v2/task?method=create&" + params.toString();
handles encoding automatically and no loops needed.
The problem is $.param() doesn’t exist in Zapier’s environment - jQuery isn’t loaded. You’ll need to build the query string manually from your action fields. Here’s what worked for me:
var ZapierApp = {
task_before_request: function(bundle) {
var baseUrl = "https://xyz.api.service.com/v2/task?method=create";
var queryParams = [];
// Add action fields as query parameters
for (var key in bundle.action_fields) {
if (bundle.action_fields.hasOwnProperty(key)) {
queryParams.push(encodeURIComponent(key) + "=" + encodeURIComponent(bundle.action_fields[key]));
}
}
// Add existing params like token
for (var param in bundle.request.params) {
if (bundle.request.params.hasOwnProperty(param)) {
queryParams.push(encodeURIComponent(param) + "=" + encodeURIComponent(bundle.request.params[param]));
}
}
bundle.request.url = baseUrl + "&" + queryParams.join("&");
return bundle.request;
}
};
This builds the query string manually and encodes everything properly - should get you the URL format you need.
You’re accessing bundle.request.action_fields but action_fields sits at the top level of the bundle object, not under request. Use bundle.action_fields instead. Hit this exact same problem last month on a similar integration. Here’s how your code should look:
var ZapierApp = {
task_before_request: function(bundle) {
var url = "https://xyz.api.service.com/v2/task?method=create";
// Build query string from action_fields
var params = [];
Object.keys(bundle.action_fields).forEach(function(key) {
params.push(key + "=" + encodeURIComponent(bundle.action_fields[key]));
});
// Add token from existing params
if (bundle.request.params.token) {
params.push("token=" + bundle.request.params.token);
}
bundle.request.url = url + "&" + params.join("&");
return bundle.request;
}
};
Basically, action_fields and request are siblings in the bundle structure, not parent/child.