I have some code in a workflow but it seems not to like to drill down to the array of an array in a JSON input record.
const data = query_ClickUp_View_Page.data.tasks;
var jsonArray = [];
var jsonObject;
var i = 0;
var id = "";
var custom_id = "";
var po = "";
var customer_id_array = [];
var customer_id = "";
for (i == 0 ; i < data.length ; i ++) {
id = data[i].id;
custom_id = data[i].custom_id;
po = data[i].custom_fields.filter(row => row.id == "111")[0].value;
customer_id_array = data[i].custom_fields.filter(row => row.id == "111)[0].value;
/*customer _id = customer_id_array. <nothing works>
customer_id = customer_id_array; // this works
jsonObject = {'order_id': id, 'order_number': custom_id, 'customer_po': po, 'customer_id': customer_id};
jsonArray.push(jsonObject);
}
return jsonArray
customer_id is an array, so you'll want to access the [0] index to access the values. You could use find instead of filter which would return the first instance as an object, instead of an array of found value (if you know there is only ever 1 to find).
You can also simplify your JS a bit into something like this, which will use map to iterate over your tasks, building the result into an array.
return query_ClickUp_View_Page.data.tasks.map( task => { // Map will return an array of processed data
const {id, custom_id} = task // Pull those 2 values right out of the task data
const po = task.custom_fields.find(row => row.id === '111').value // I'm not sure what this is or how it differs from the customer_id_array
const customer_id = task.custom_fields.find(row => row.id === '111').value.id // access the value.id property of the found data
return {id, custom_id, po, customer_id
}
Thanks @MikeCB for your response and for changing upgrading my code. i am not a developer, and my brain is always thinking of divide and conquer instead of doing everything at once. Great learning opportunity here...
Anyhow, I have done everything to access this array and is not possible...
I just tried your suggested code and I get the same than I getting when I try to get any content of that array.
{"data":null,"error":"TypeError: Cannot read properties of undefined (reading 'id') (line x)"}
Even with my code I get the same error... it only works if i don't try to get anything from the customer array. The only thing that works is if I only get the value: const customer_id = task.custom_fields.find(row => row.id === '111').value
Anything after the value fails.
I have even tried getting that json on another code block, etc.
I decided to create a new workflow, and have the test data as my input as shown on my original post. The issue is data related, nothing related to the code. When I get a couple of records from the array it works perfectly, but when I get the whole dataset (100 records) it doesn't like it.
Sigh!!! The wrong assumption was made... from all the dataset (more than 1k records ONLY one doesn't have the customer id array, so it cannot evaluate the content of a non existing array.
Here is the code that ended doing exactly what I needed with always having the 4 elements with the desired names of each json row: