ForEach only works for the first item
See original GitHub issueHi,
I’ve been trying to get a for-each loop to work that just writes the items of a list to the console.

The Dummy Data Generator returns a List<Dictionary<string, string>> as Output.

I use Javascript: (input) for both the ForEach Items param and the WriteLine Text param.
The expected outcome is that all my list entries are printed to the console, but what actually happens is that the first entry is successfully printed and then an exception is thrown:

Hopefully somebody can help me with this 😃
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Why is the ForEach only registering the first element from ...
The querySelector returns a static NodeList. So when this code is run, probably only one element is rendered in the page.
Read more >Foreach is only returning the first value
I am currently building a store and i am using Crinsane package but i found out that after trying to iterate over the...
Read more >Why the forEach JavaScript Method Might Not Always be ...
In the first case, forEach wouldn't work at all since there's no way to exit it, and in the latter, you might get...
Read more >Why is it that the first Element is returned in forEach loop?
The forEach method actually has no return value so it will always return undefined. Try using .map() instead. Posting to the forum ...
Read more >How to treat first item in a foreach (or similar) different than the ...
Simply put, it checks if there is just one organization for each candidate row. If it's only one, then it copies that organization...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I see now. The issue is the use of the
inputvariable, which changes over time.For example, initially,
inputwill hold the list of dictionaries (the output of the custom activity). Next, as theForEachactivity executes, it changes theinputvalue to the item of the current iteration (a dictionary).Once an iteration completes,
ForEachis executed again, which will re-evaluate itsItemsproperty. But since this expression usesinput, it is no longer the original list of dictionaries.To fix this, you need to perform the following steps:
Output).context.Output, set the result to the newOutputproperty.ForEachitems JS expression to:activities.TestDataSource1.Output().WriteLineand theForEachactivity; Elsa will automatically execute theForEachactivity for you after itsIteratebranch completes.Here is the updated activity code:
And here is the updated workflow JSON:
I will close this issue as solved, but feel free to follow-up with any questions or concerns.
I upgraded all Elsa packages to 2.3.0 but it still throws the same exception. Here is the exported Workflow JSON:
{ "$id": "1", "definitionId": "371ae20403c34965be434a5584216ad5", "versionId": "060cc3b9ad5d469ba7997dbba772ae53", "name": "loop_test_data", "displayName": "Loop test data", "version": 10, "variables": { "$id": "2", "data": {} }, "customAttributes": { "$id": "3", "data": {} }, "isSingleton": false, "persistenceBehavior": "WorkflowBurst", "deleteCompletedInstances": false, "isPublished": true, "isLatest": true, "activities": [ { "$id": "4", "activityId": "e8c51f8c-1118-4a07-9db0-10dfd70e4ee5", "type": "HttpEndpoint", "displayName": "HTTP Endpoint", "persistWorkflow": false, "loadWorkflowContext": false, "saveWorkflowContext": false, "properties": [ { "$id": "5", "name": "Path", "expressions": { "$id": "6", "Literal": "/loop-test" } }, { "$id": "7", "name": "Methods", "expressions": { "$id": "8", "Json": "[\"GET\"]" } }, { "$id": "9", "name": "ReadContent", "expressions": { "$id": "10" } }, { "$id": "11", "name": "TargetType", "expressions": { "$id": "12" } } ], "propertyStorageProviders": {} }, { "$id": "13", "activityId": "32e64fb2-94ad-40a0-9f4e-2d6f1df771d9", "type": "TestDataSource", "displayName": "Test", "persistWorkflow": false, "loadWorkflowContext": false, "saveWorkflowContext": false, "properties": [ { "$id": "14", "name": "EntryAmount", "expressions": { "$id": "15", "Literal": "50" } }, { "$id": "16", "name": "Columns", "expressions": { "$id": "17", "Literal": "5" } } ], "propertyStorageProviders": {} }, { "$id": "18", "activityId": "4e346ab0-bcb0-4d5f-b175-0a5739f53e85", "type": "WriteLine", "displayName": "Write Line", "persistWorkflow": false, "loadWorkflowContext": false, "saveWorkflowContext": false, "properties": [ { "$id": "19", "name": "Text", "syntax": "JavaScript", "expressions": { "$id": "20", "JavaScript": "(input)" } } ], "propertyStorageProviders": {} }, { "$id": "21", "activityId": "439b8305-22a7-4d87-9af6-6fe9c17879af", "type": "WriteLine", "displayName": "Write Line", "persistWorkflow": false, "loadWorkflowContext": false, "saveWorkflowContext": false, "properties": [ { "$id": "22", "name": "Text", "expressions": { "$id": "23", "Literal": "SUCCESS" } } ], "propertyStorageProviders": {} }, { "$id": "24", "activityId": "7b1a127b-7da6-4006-af17-754dcd64878c", "type": "ForEach", "displayName": "For Each", "persistWorkflow": false, "loadWorkflowContext": false, "saveWorkflowContext": false, "properties": [ { "$id": "25", "name": "Items", "syntax": "JavaScript", "expressions": { "$id": "26", "Literal": "(input)", "JavaScript": "(input)" } } ], "propertyStorageProviders": {} } ], "connections": [ { "$id": "27", "sourceActivityId": "e8c51f8c-1118-4a07-9db0-10dfd70e4ee5", "targetActivityId": "32e64fb2-94ad-40a0-9f4e-2d6f1df771d9", "outcome": "Done" }, { "$id": "28", "sourceActivityId": "32e64fb2-94ad-40a0-9f4e-2d6f1df771d9", "targetActivityId": "7b1a127b-7da6-4006-af17-754dcd64878c", "outcome": "Done" }, { "$id": "29", "sourceActivityId": "7b1a127b-7da6-4006-af17-754dcd64878c", "targetActivityId": "4e346ab0-bcb0-4d5f-b175-0a5739f53e85", "outcome": "Iterate" }, { "$id": "30", "sourceActivityId": "7b1a127b-7da6-4006-af17-754dcd64878c", "targetActivityId": "439b8305-22a7-4d87-9af6-6fe9c17879af", "outcome": "Done" }, { "$id": "31", "sourceActivityId": "4e346ab0-bcb0-4d5f-b175-0a5739f53e85", "targetActivityId": "7b1a127b-7da6-4006-af17-754dcd64878c", "outcome": "Done" } ], "id": "060cc3b9ad5d469ba7997dbba772ae53" }And the code for my Activity:
And my custom Exception:
@sfmskywalker I hope you can reproduce the error with this 😃