Wednesday, October 6, 2021

PowerApps/Power Automate: Send List Data To PowerApps And Convert To Collection

 Hello Friends,

Welcome back with another post on PowerApps with Power Automate. In real time scenarios, we often face the problem where we have to get some filtered data from data source and to display in PowerApps Gallery.

One way it could be we can use delegation but sometimes it doesn't meet our requirements. In those scenarios, we have to move on towards Power Automate, which could return us a data result and we can show it in gallery.

It sounds good but practically it has challenges like, we have no option to return table as output to PowerApps. We can return data of type Text, Yes/No, File, Email, Number, Date.

You may be thinking that we can parse the result set in JSON and return it as text. 😊

Yes, you can but the returned content is of type text and it will be treated as plain string. So, What Next?

There is a trick through which we will send data to PowerApps and will convert into collection. Let's see how-

  1. Suppose I have a list called Quote where I am managing daily display quotes. It has primarily 2 columns-
    1. DisplayDate
    2. Quote
  2. I need to fetch these quotes from list and show in a gallery in PowerApps. I am using Power Automate.
  3. So, let's start with making flow in Power Automate. Open the Power Automate platform and click on + Create and choose Instant cloud flow. Give it a lovely name and choose the trigger type as PowerApps.

  4. Click on Create to get the flow created.
  5. Now click on + New step and add an action Get Items (SharePoint). Choose the SharePoint site in Site Address box. Then choose respective list in List Name box.

  6. From this data, we need only DisplayDate and the Quote to be sent to PowerApps. So, we will initialize a variable and concatenate this data using two separate delimiters-
    1. ## for separating the data (DisplayDate##Quote)
    2. $$ for separating the rows (DisplayDate##Quote$$DisplayDate##Quote)
  7. For this use Initialize variable and then use Apply to each action.

  8. In Apply to each action, use Append to string variable to append the concatenated value to the variable.
  9. This way, we have concatenated all rows. But the problem is that an additional instance of row delimiter ($$) is concatenated at the end of this string. So we have to remove it. For that, we will use the substring function.
  10. Choose a Compose action and assign the substring value to it.
  11.  
  12. Now, the last part of this flow is to return this data back to PowerApps. For this, add an action Respond to a PowerApp of flow.
  13. Choose Output as Text.
    1. Enter the title- "ResponseResult".
    2. Enter a value to respond- vrConcatenatedData

  14. That's all in Power Automate. You may test it manually-
  15. Now we will create PowerApps.
  16. Create a Canvas PowerApp.
  17. Add a button-
    1. Name: btnGetQuotes
    2. Display Text: Get Quotes

  18. Now we will use the OnSelect property of this button to apply our functionality.
  19. First of all, we will fetch the data from Power Automate to a variable. For this choose the OnSelect property of button and click in the function box then click on Action >> Power Automate >> GetQuotesForPowerApps.
  20. It will add this flow in function box of OnSelect property.
  21. Complete this function as below-
  22. Set(
        varResponseResult,
        GetQuotesForPowerApps.Run().responseresult
    );
    
  23. Now we will split this data based on Row Delimiter ($$).
  24. ClearCollect(
        collResponseResultSplitRowDelimited,
        Split(
            varResponseResult,
            "$$"
        )
    );
    

  25. Now execute the button to get the collection loaded.
  26. As we can see, the collection is loaded with each row. Now, we will split each row with data delimiter (##).
  27. ForAll(
        collResponseResultSplitRowDelimited.Result,
        Collect(
            collResponseResultFinal,
            {
                ID: First(
                    Split(
                        Result,
                        "##"
                    )
                ).Result,
                Title: Last(
                    Split(
                        Result,
                        "##"
                    )
                ).Result
            }
        )
    );
    


  28. Thus, complete code is-
  29. Set(
        varResponseResult,
        GetQuotesForPowerApps.Run().responseresult
    );
    ClearCollect(
        collResponseResultSplitRowDelimited,
        Split(
            varResponseResult,
            "$$"
        )
    );
    ClearCollect(
        collResponseResultFinal,
        []
    );
    ForAll(
        collResponseResultSplitRowDelimited.Result,
        Collect(
            collResponseResultFinal,
            {
                ID: First(
                    Split(
                        Result,
                        "##"
                    )
                ).Result,
                Title: Last(
                    Split(
                        Result,
                        "##"
                    )
                ).Result
            }
        )
    );
    
  30. The output is-
  31. Lastly, I had bind this collection to a gallery.
  32. This way, we can achieve the desired functionality.
  33. UPDATE:
  34. In case, if you need to send more than 2 columns from Power Automate then we need to make some changes in PowerApps to split using data delimiter. Let's suppose, we need to send the SNo column also. Then, first, we will make changes in Power Automate as (we are using the Title column because the internal name of this column is Title) below-
  35. concat(items('Apply_to_each')?['DisplayDate'],'##',items('Apply_to_each')?['Quote'],'##',items('Apply_to_each')?['Title'],'$$')
    
  36. Now, we know that the sequence of data in each row is as-
    1. DisplayDate
    2. Quote
    3. SNo
  37. But we want to display in below format-
  38. SNo
  39. DisplayDate
  40. Quote
  41. Therefore, we will make changes in the last ForAll function which is written on OnSelect property of btnGetQuotes button
  42. ForAll(
        collResponseResultSplitRowDelimited.Result,
        Collect(
            collResponseResultFinal,
            {
                SNo: Last(
                    Split(
                        Result,
                        "##"
                    )
                ).Result,
                DisplayDate: First(
                    Split(
                        Result,
                        "##"
                    )
                ).Result,
                Title: Last(
                    FirstN(
                        Split(
                            Result,
                            "##"
                        ),
                        2
                    )
                ).Result
            }
        )
    );
    
  43. As the SNo was appearing at last of the item row, hence we fetched it using Last function.
  44. DisplayDate was appearing at first of the item row, hence we fetched it using First function.
  45. Quote is now appearing in the middle of the item row at position 2 hence we have used Last with FirstN function. First we fetched First 2 records. It will return DisplayDate & Quote, then we applied Last function, so as the Quote is at the last of this result set, we will get Quote.
  46. This way we can send as much as data from Power Automate and get it displayed in PowerApps.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.