- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 08:24 PM
I noticed some strange behaviour with a flow variable of type JSON, that contains an array but then becomes null when all items in the array have been removed. I was expecting it to simply remain an array with length of 0. To illustrate here is a simplified example in flow designer:
11 - Array with 2 items
12 - Remove one item from the array - result is still an array
13 - Remove another item from the array - result is no longer an array but a null object
14 - Fails as the flow variable is now a null object, therefore fd_data.flow_var.list.length fails.
Of course there's ways around this and I have my flow working fine, but I wondered if this should be reported as a bug?
Ron
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 09:17 PM
Hi @Ron28
Yes, this behavior is expected and not a bug. When all items are removed from a JSON array, the array itself is removed and the variable becomes null. This is because JSON arrays are represented as objects in Flow Designer, and when all properties are removed from an object, the object itself is removed. Therefore, when all items are removed from a JSON array, the array itself is removed and the variable becomes null.
One way to work around this is to use a default value for the variable. For example, you could set the default value to an empty array. This way, when all items are removed from the array, the variable will still contain an empty array and will not become null.
Another way to work around this is to use a different data type for the variable. For example, you could use a string variable and store the array as a JSON string. This way, when all items are removed from the array, the variable will still contain a JSON string and will not become null.
Mark helpfull if this answers your query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 09:17 PM
Hi @Ron28
Yes, this behavior is expected and not a bug. When all items are removed from a JSON array, the array itself is removed and the variable becomes null. This is because JSON arrays are represented as objects in Flow Designer, and when all properties are removed from an object, the object itself is removed. Therefore, when all items are removed from a JSON array, the array itself is removed and the variable becomes null.
One way to work around this is to use a default value for the variable. For example, you could set the default value to an empty array. This way, when all items are removed from the array, the variable will still contain an empty array and will not become null.
Another way to work around this is to use a different data type for the variable. For example, you could use a string variable and store the array as a JSON string. This way, when all items are removed from the array, the variable will still contain a JSON string and will not become null.
Mark helpfull if this answers your query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 10:40 PM
Thanks for the explanation. I used the save javascript code and ran it outside of ServiceNow which worked as I expected, where I ended up with an array with a length of 0. I understand an array is still an object.
Ron