Looking for advice on why my substring logic is breaking in the Architect expression editor.
I am trying to format a phone number in a Genesys Cloud Architect flow. The incoming data from our webhook is a raw string like +15551234567. I need to transform this into (555) 123-4567. I wrote the following expression: "(" + substring(data.phoneNumber, 2, 5) + ") " + substring(data.phoneNumber, 5, 8) + "-" + substring(data.phoneNumber, 8, 12) but it throws a validation error saying the arguments are invalid or the result is null. Am I missing a specific function for this or is my index math wrong?
You need to verify the exact string indices because Architect’s substring function is zero-indexed and the second parameter is the end index (exclusive), not the length. If your input is +15551234567, index 0 is +, 1 is 1. The area code starts at index 2.
Wait, that looks right syntactically. The issue is likely that data.phoneNumber is not a string or contains hidden whitespace. In my Electron integrations, I often sanitize inputs before passing them to GC. Ensure the webhook payload explicitly casts the value. Also, check if the flow variable type is set to String. If it is Number, substring will fail. Cast it first: toString(data.phoneNumber).
Make sure you validate the input length before slicing. I often see webhook payloads carry hidden whitespace or varying country codes that break static indices. Use trim(data.phoneNumber) to sanitize first, then log the result with a Set Variable action to inspect the intermediate string.
If the source is unreliable, regex is safer. Try replace(data.phoneNumber, /(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"). This handles the formatting without manual index math, reducing the chance of off-by-one errors in Architect.