I have a js query that is triggered when a cell in a table is edited.
// Initialize the variables
let routeId = null;
let jobId = null;
// Check if changesetArray exists and is not empty
if (tableLocations.changesetArray && tableLocations.changesetArray.length > 0) {
// Get the Route Number and Job ID from the selected row in tableLocations
const routeNumber = tableLocations.changesetArray[0].routeId;
jobId = tableLocations.changesetArray[0].id;
// If the routeNumber is not "Unassigned" or null, set routeId to the routeNumber
if (routeNumber !== 'Unassigned' && routeNumber !== null) {
routeId = routeNumber;
}
}
// Trigger the SQL query if jobId is available
if (jobId) {
updateRouteId.trigger({
additionalScope: {
routeId: routeId,
jobId: jobId
},
onSuccess: function(data) {
console.log('Update successful:', data);
},
onFailure: function(error) {
console.error('Update failed:', error);
}
});
} else {
console.error('No valid changeset available.');
}
When I hover over the additional scope, I can see the values
Here is my update query:
-- Temporary debug query
SELECT {{ routeId }} AS passedRouteId, {{ jobId }} AS passedJobId;
-- Actual update query
UPDATE public.jobs
SET route_id =
CASE
WHEN {{ routeId }} IS NULL THEN NULL
ELSE (
SELECT id
FROM public.routes
WHERE route_number = {{ routeId }}::integer
)
END
WHERE id = {{ jobId }}::uuid;
Additional scope is set:
When I try to update the cell the query throws the following error: could not determine data type of parameter $1
Just to confirm that the id and routeId are available in your changesetArray, you have set the table to "Include full rows in changeset Array" correct?
You should be able to do some additional debugging by adding something like: console.log({jobId, routeId}) right before if(jobId) and then checking the console to ensure those values are populated as expected.
oh I mean with the ' ' around the { }, not 'Parameter1'... for u '{{ routeId }}' and '{{ jobId }}', if you're using copy/paste be sure to get the single quotation marks
bummer, then i think the problem is ::uuid, try this:
WHERE id::text = {{ jobId }}::text
additional scope variables are all passed as string, so even if on the calling side it's UUIDv4 or something it's automatically cast to string before it's used in the call to the query.
That didn't work either. I did find a way to use a query with a js query in the meantime. Would have been nice to know how how to pass the variables though. Even with the js query it seems like overkill.
Query:
UPDATE public.jobs
SET route_id =
CASE
WHEN COALESCE({{ transformRouteID.data }}, NULL) IS NULL THEN NULL
ELSE (
SELECT id
FROM public.routes
WHERE route_number = {{ transformRouteID.data }}::integer
)
END
WHERE id = {{ tableLocations.changesetArray[0].id }}::uuid;
Transformer:
// Check if changesetArray is null or empty
if (! tableLocations.changesetArray || tableLocations.changesetArray.length === 0) {
return null; // Early return to prevent further processing
}
// Get the Route Number from the selected row in tableLocations
const routeNumber = tableLocations.changesetArray[0].routeId ;
// If the routeNumber is "Unassigned", return null, otherwise return the routeNumber itself
const routeId = (routeNumber === 'Unassigned' || routeNumber === null) ? null : routeNumber;
// Return the result
return routeId;