Copilot Studio does not provide functions like ClearCollect or Collect, but since there are many situations where you want to dynamically add items, I investigated the method to achieve this.
What We Want to Do
This time, the focus is on performing a process similar to this in Power Apps:


Since Copilot Studio does not have collections like ClearCollect or Collect, this can be achieved by updating a variable of the Table type.
Method 1: Updating with the Table Function
First, initialize a variable of the table type. Ensure that you add at least one object during initialization.
* Adding an “empty table” during initialization will not work properly (refer to the “Bonus” section for more details).
* The object will be removed later, so any value is fine. However, make sure that the properties match those of the items added within the loop.

* Adding an “empty table” during initialization will not work properly (refer to the “Bonus” section for more details).
* The object will be removed later, so any value is fine. However, make sure that the properties match those of the items added within the loop.

Next, update this variable using the Table function within a loop to dynamically add items. ※ For more details on building a loop, refer to this article.


After the loop completes, use the LastN function to remove the object that was initially added.


This provides the required table (object array).


Method 2: Using JSON
You can also achieve this using JSON. Start by defining an empty string as the initial value.


Within the loop, use the JSON function to create object strings, and concatenate them with commas in between.


Topic.json_Collection & If(!IsBlank(Topic.json_Collection), ",") & JSON({num:Topic.i})
Finally, enclose the string with “[” and “]”, then use ParseJSON and ForAll to convert it into a table.


ForAll( ParseJSON("[" & Topic.json_Collection & "]"), {num: Int(ThisRecord.num)} // Define the object )
This is how you can obtain a table (object array):


If you need to dynamically add values received via JSON from external services, this method may be more convenient.
Bonus: Initialization of Variables with an Empty Table Does Not Work Properly
When you define a table-type variable as empty:


And then add items to it using the same approach as before:


The result will unexpectedly contain `null` values:


Even if you change the first argument of the `Table` function to `ForAll`, the result remains the same:


Interestingly, this process works correctly in Power Apps, so this seems to be a specification of Power Fx within Copilot Studio:


コメント