Using Foreach Loops in Copilot Studio Conversations: Simplify Table and Array Operations
While researching loops in Copilot Studio, I came across an interesting article that I’d like to share.
スポンサーリンク
How to Create Loops in Conversations
In Copilot Studio, loops within conversations were previously constructed using the “Go to step” node.
This functions similarly to the goto statement in languages like C, where you can create a loop by returning to a previous step using the “Go to step” action.
However, in the article I found, it mentioned that you can use Foreach loops within topics. So, I decided to give it a try.
As an example, this topic demonstrates the construction of a Foreach loop to execute a specified number of iterations.
First, set up an appropriate trigger and use slot filling to receive the number of iterations for the loop.
Next, open the code editor to add the Foreach loop.
Within the `actions` section, add a `kind` property for Foreach (similar to YAML syntax).
You can assign any ID to the loop, so pick an arbitrary ID, save it, and close the code editor.
Once completed, the Foreach loop is added to the flow.
To execute the specified number of iterations, use the `Sequence` function in the [Items to Loop Through] setting.
For the [Variable for Loop Values], add a new variable to store each loop item.
※The loop index can also be retrieved from the properties.
Lastly, you might want to add a node to send the current number as a message. However, it seems that the [Select Variable] interface does not provide access to the `i` variable.
To resolve this, reopen the code editor and manually input the function and variables directly.
Return to the flow, and if the function is correctly input, the setup is complete.
Behavior Check
When you specify the number of times like this, the loop runs within the topic.
It’s unclear whether this is a leftover feature from Power Virtual Agents or a function that will officially be added to Copilot Studio in the future, but you can currently use Foreach if you access it through the code editor.
コメント