Tip: Use JavaScript to filter on a value in a record array
Use the following JavaScript code to filter records on the presence or absence of a given value in a record array. When applying this filtration strategy, you must enable the JavaScript option in order to add the function. Click the JavaScript toggle filter at the top of the filter editor.
Here is an example record:
{
"record": {
"orderId": 123,
"total": 99.99,
"state": "ca",
"items": [
{
"description": "Hat",
"qty": 3
},
{
"description": "Shoes",
"qty": 1
}
]
}
}
Here is the JavaScript function:
function filter(options) {
for (var i = 0; i < options.record.items.length; i++) {
if (options.record.items[i].description === "Hat") {
return false;
}
}
return true;
}
If the function is set to "false" the record will be ignored. If the function returns "true" the record will be processed.
2
Comments
Thank you, Tom! Question: is the filter either J-Script or Rule but not both? I'm guessing that's they are mutually exclusive, but just wanted to verify.
Jim
I am pretty sure you can only use one or the other not both.
Thanks for this helpful code, this is an alternative filter searching for different criteria using reduce()
Please sign in to leave a comment.