Use an Array.Object in flow designer javascript
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 04:45 AM - edited 08-30-2023 05:16 AM
I've got a catalog item using a multiline variable. During the processing of the RITM, a summary mail should be sent, so I should be able to list all the lines from the multiline.
Problem: I'm want to use a forEach in the send email action, but when I try to use the Array.Object in my script, the forEach throws an error, and the variable is only recognised as a simple object, not an array. (I used typeof to verify).
Array:
[{
"Name" : "One",
"Mail" : "one@one.com",
}, {
"Name" : "Two",
"Mail" : "two@two.com",
}, {
"Name" : "Three",
"Mail" : "three@three.com",
}]
Code:
var mailBody = "New request for external visitors<br /><table><tr><td><b>Participants</td><td><ul>";
var allVisitors = fd_data._1__get_catalog_variables.participants
allVisitors.forEach((visitor) =>
mailBody += "<li>" + visitor.Name + "</li>"
);
mailBody += "</ul></td></tr></table>";
return mailBody;
If I just try adding the allVisitors variable to the mailBody, it shows as [object Object],[object Object],[object Object]
Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 05:53 AM
I guess the issue you're encountering is because fd_data._1__get_catalog_variables.participants is being treated as a JSON object, not an array.
To make sure it's recognized as an array and can be used with the forEach loop, you need to parse it into an array.
var mailBody = "New request for external visitors<br /><table><tr><td><b>Participants</b></td><td><ul>";
var allVisitors = Object.values(fd_data._1__get_catalog_variables.participants);
allVisitors.forEach((visitor) =>
mailBody += "<li>" + visitor.Name + "</li>"
);
mailBody += "</ul></td></tr></table>";
return mailBody;
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 06:05 AM
Looks like it somehow uses the array wrong. using Object.values doesn't change anything. Still getting syntax error.
I did a workaround for this by assigning a flow variable in a for each loop. This works but it's just a lot more bulky than getting a simple foreach to work in JS...