Transformer working but throwing error when null

Hi everyone,

I have an update query triggered when I update a cell in a table. It uses a transformer to set a value to ```null`` if the option in the cell is "Unassigned".

// Initialize routeId as null
let routeId = null;

// Check if changesetArray exists and is not empty
if ({{ tableLocations.changesetArray }} && {{ tableLocations.changesetArray.length }} > 0) {
  // Get the Route Number from the selected row in tableLocations
  const routeNumber = {{ tableLocations.changesetArray[0].routeId }};
  
  // If the routeNumber is "Unassigned", keep routeId as null, otherwise set routeId to the routeNumber
  if (routeNumber !== 'Unassigned' && routeNumber !== null) {
    routeId = routeNumber;
  }
}

// Return the result (either null or the routeNumber)
return routeId;

The query uses the variable and works during the update.

UPDATE public.jobs
SET route_id = 
  CASE 
    WHEN COALESCE({{ transformRouteID.value }}, NULL) IS NULL THEN NULL
    ELSE (
      SELECT id 
      FROM public.routes 
      WHERE route_number = {{ transformRouteID.value }}::integer
    )
  END
WHERE id = {{ tableLocations.changesetArray[0].id }}::uuid;

It seems normal the transformer is throwing an error when I am not changing a value in the table as there is no changesetArray.

I actually just figured out how to do the query without the transformer. I'm just curious if there is a best practice when referring to a changesetArray and the value is potentially empty.

Hello @Shawn_Optipath great to hear you were able to get it working without a transformer!

If you have changesetArray in an if statement it should evaluate to null or length of zero and fail as you specified.

What part of the transformer was throwing the error? If the error is coming from the reference to changesetArray in the if statement I can definitely look to see what the best work around is for avoiding the error.