Convert an array of objects to a comma separated string using Power Automate

Byron Dittberner | Featured image

In my last post I showed how to create a multiselect lookup. While this feature is pretty cool, when adding the field to a view, the data is shown as an array of objects which will not work from a readability standpoint. In this post I will show you how to convert an array of objects to a comma separated string using Power Automate so that the data can be displayed in a view.

Byron Dittberner | Converting an array of object to comma separated sting
Byron Dittberner | Account showing as objects in an array.

You can see from the image on how the accounts are displayed when adding the new multiselect lookup to a view. Of course, this will not be feasible. It would be much better to have the accounts displayed as Account 1, Account 2, Account 3…

Of course, there are several ways to do this, however, I am going to show you how to convert the array of objects and store them in a readable comma separated sting in a new column using Power Automate (Flow). The final result will have a multiselect lookup on the contact form and a new column called companies displaying in the contact view in a readable format.

Before we get started with the Flow, you will first need to create a new column that will store the reformatted account names. So, create a column called Associated Accounts, set the data type to multiline text and the maximum length of characters under advanced settings should to 4000 characters.

Byron Dittberner | Create a column titled associated accounts
Byron Dittberner | Set the advanced settings as per the image

Next, add the Associated Accounts column to the Active Contacts view

Byron Dittberner | Add the associated account column to the active contacts view

Create a Flow to convert the array of objects to a comma separated string

Now that we have the basics set up, we can now move onto creating the Flow that will convert the selected accounts into a readable format.

First, you will need to copy the name of the account’s column from the contact table as your will need it for the next step.

Byron Dittberner | copy the field name

Create a new automated cloud flow and select Dataverse for your trigger. Set the change type, label name and scope as per the image. Finally, paste the accounts field name into the select columns field. This tells the flow to only trigger when the accounts field is modified.

Byron Dittberner | add a dataverse trigger

Add a Get a row by ID action and set the Row ID to the dynamic value of Contact.

Byron Dittberner | add a dataverse get row by ID action

Add a compose action and set the input the dynamic value for Accounts. Be sure to select the dynamic value from the Get a row by ID action.

Byron Dittberner | Add a compose action to extract the accounts object

Add an Initialize variable action. In the step we will take the output from the compose action and convert it to an array of JSON objects. You may be wondering why since the data already looks like it is formatted? The issue is the data output from the compose action is of type string and we need it in an actual array so that we are able to reach each object within the array. To set the value, you will need to write an expression.

Byron Dittberner | convert the output from a compose action to a JSON array
Byron Dittberner | write the expression that will format the compose output
Use the following expression json(output(‘compose’))

Before you move onto the next step, you need to run a test on your flow and copy the output from the variable. We will use the output to define the schema in the next action. On the top right of your screen, click on Save, then click on test. Wait for the test session to start, the edit on of the contact records by associating a few accounts. Once you click on save, your Flow should fire.

Byron Dittberner | Save and Test the Flow to get the schema
Save and test you Flow
Byron Dittberner | Select a few accounts to associate to the contact record
Select a few accounts

Now that your Flow has successfully run, you need to copy the output from the variable as you will need it to define the schema for the next action. Once the Flow has run, click on the variable to view the output and copy all the contents.

Byron Dittberner | Copy the variable output to use as the schema for the next action
Copy everything from the Value field.

The next action we will add the Parse JSON action. This action will convert the sting of accounts into a format called JSON so that we are able to reach each of the account names from the object.

Byron Dittberner | add a parse json variable
Use the Parse JSON action and set the content to the variable output from the previous step. Be sure to use the dynamic output.

Next you need to click on Generate from Sample. when the dialog box opens, paste the output from the variable in the previous step into the box the click on done. This will generate the schema required to convert the variable output the JSON.

Byron Dittberner | paste the payload from the variable output
Paste the output from the variable and the click on done.
Byron Dittberner | The result after generating the schema from the payload
the final result after generating the schema from the sample payload.

For the next action, you will initialise another variable, this will be to hold the newly comma separated account names. Add the action, initialise variable and set the properties as per the image.

Byron Dittberner | Add the action the initialise a variable to store the comma separated accounts
Add an initialise variable action

In this step, you will loop through each of the objects and extract the name value followed by appending each name to the var_associatedaccounts variable. Add an Apply to each action and a compose action within in Apply to each. Set the properties as per the image.

Byron Dittberner | add an apply to each action to loop through each object within the array.

Continuing on from the previous step, add an Append to string variable and set the name to the associated accounts variable (var_associatedaccounts). For the value, you will need to write the following expression.

concat(outputs(‘compose_2′),’, ‘)

This expression is what creates the comma separated account names.

Byron Dittberner | add an append to string variable action
Add an Append to sting variable action
Byron Dittberner | use the concat expression to join the account names together
Use the concat expression to set the value of the append to variable string.

Finally for the last action you will now update the contact record with the newly created comma separated account names. Add a Dataverse Update a row action. Make sure to add the action outside of the Apply to each as shown in the image. Set the table name to contacts and the row id to the dynamic value of Contact (this is the record id for the contact).

Byron Dittberner | add an update a row action and set the table name and row id
Set the table name and row id
Byron Dittberner | expand the advanced options
Scroll to the bottom of the update a row action and expand the advanced options.
Byron Dittberner | Set the associated accounts to the variable for associated accounts
Set the associated accounts to the dynamic variable you created, var_associatedaccounts

And that’s a wrap. you have managed to convert an array of objects to a comma separated string using Power Automate. Save and test your Flow.

The final result

Byron Dittberner | The final result
Convert an array of objects to a comma separated string using Power Automate

The final result of this solution provides a way for a user to select multiple accounts on the contact record and then using a flow you remote the output into a readable format that can be added to a view. In essence, you have two columns, one for a form and one for a view.

I hope you found this article helpful.

Until next time, happy CRMing!

Byron Dittberner profile picture
Byron Dittberner | Dynamics 365 & Power Platform Solution Architect

Leave a Reply

Discover more from Dynamics 365 & PowerApps Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading