Hello Friends,
Welcome back with another post on Power Automate. In last 4 posts, we were discussing about CRUD operations using REST API in Power Automate with PowerApps. There, we had learnt about Insert, Update and Delete the data in SharePoint. In last post, we have learnt about to get the data in Power Automate using REST API and convert into string and send back to PowerApps. You may visit my these posts by clicking on below link-
- Power Automate: CRUD Operations Using REST API - Part 1
- Power Automate: CRUD Operations Using REST API - Part 1
- Power Automate: CRUD Operations Using REST API - Part 3
- Power Automate: CRUD Operations Using REST API - Part 4
- We have created the Power Automate and tested successfully in last post. Now open PowerApps. Then insert a new List screen by clicking Insert >> New Screen >> List.
- A new screen will be added. Rename this screen as "CustomListScreen". One vertical gallery is also added automatically on this screen. Rename it "CustomListGallery".
- Change the display title text from [Title] to "ListForRESTAPI".
- Add a button at the bottom of screen. Rename it "btnGetData" and the display text as "Get Data".
- Now take a break for 1 minute and discuss, how we will achieve the target. We have the string. We will-
- Split this string based on Item delimiter "|" (pipe sign) and store in a collection "HTTPCollection". We are using "HTTPCollection" as name because this is the data we received from HTTP request
- Get the count of records in "TotalRecords" variable by making a count on "HTTPCollection".
- Now split each item using Value delimiter "~" (tild sign) and store in a collection "TotalIterations". Here we will have 2 columns-
- SplittedData (It's a collection of splitted values Title & PinCode. Each collection has 2 rows: 1-Title Value; 2-PinCode Value)
- Value (It's a sequence number from 1 upto TotalRecords)
- Then we will use ForAll with Collect to create the final collection named "FinalHTTPCollection".
- Then we will bind this collection to the CustomListGallery control.
- Let's begin. Click on formula box of OnSelect property of btnGetData.
- Define all collections and variables.
ClearCollect(HTTPCollection,[]); UpdateContext({TotalRecords: 0}); ClearCollect(TotalIterations,[]); ClearCollect(FinalHTTPCollection,[]);
- Now click on Action >> Power Automate >> HTTPCRUD-Read. It will add this Power Automate in formula box. BUT, it will remove all other code from that box so don't worry. Again add the above code as usual before that Power Automate.
- Now replace the Power Automate line with below one-
ClearCollect(HTTPCollection,Split('HTTPCRUD-Read'.Run().resultset,"|"));
- This line will get data from Power Automate, then it will split on the basis of pipe delimiter and then will be saved in HTTPCollection.
- Here ".resultset" is the Response Title we have defined in Power Automate. PowerApps takes the Response Text in lower case.
- Below image will make a clear picture about the data we captured in HTTPCollection.
- Now add below line.
UpdateContext({TotalRecords: CountRows(HTTPCollection)});
- This will update the rows count in TotalRecords variable.
- Now add below code-
ClearCollect( TotalIterations, AddColumns( Sequence(TotalRecords), "SplittedData", Split( Last( FirstN( HTTPCollection, Value ) ).Result, "~" ) ) );
- It will create a collection with named "TotalIterations" which has 2 columns: SplittedData and Value. Value column has an incremental number starting from 1 up to Total number of records. SplittedData column further has a collection (having one column named "Result") for each record. Each collection has 2 records: Value of Title, Value of PinCode.
- Below picture will make the image clear-
- the last set of code is-
ForAll( TotalIterations, Collect( FinalHTTPCollection, { Title: Last( FirstN( Last( FirstN( TotalIterations.SplittedData, Value ) ).SplittedData, 1 ) ).Result, PinCode: Last( FirstN( Last( FirstN( TotalIterations.SplittedData, Value ) ).SplittedData, 2 ) ).Result } )
);- It will iterate on each record of TotalIterations and prepare the final collection data named "FinalHTTPCollection". As the SplittedData has Title as first record and PinCode as second record, hence we have used 1 & 2 in code to fetch the same when using Last/FirstN.
- Below picture will make the image clear-
- Now we have the final output collection. First of all, Save the PowerApps and execute it and then click on Get Data button so that the data gets loaded in PowerApps.
- Now, bind this collection to the CustomListGallery DataSource. Edit the Layout if required as well as the binding of columns.
- Hurray !
- It's done. Now you can Save/Publish the PowerApps and try it in browser as well as in mobile.
- Wait...Wait...Wait...
- You have not linked this New Screen to the Home Page (BrowseScreen1).
- Click on BrowseScreen1. Add an icon (as per your wish) and update the formula for OnSelect property as-
Navigate(CustomListScreen,None);
- The complete code is:-
ClearCollect( HTTPCollection, [] ); UpdateContext({TotalRecords: 0}); ClearCollect( TotalIterations, [] ); ClearCollect( FinalHTTPCollection, [] ); ClearCollect( HTTPCollection, Split( 'HTTPCRUD-Read'.Run().resultset, "|" ) ); UpdateContext({TotalRecords: CountRows(HTTPCollection)}); ClearCollect( TotalIterations, AddColumns( Sequence(TotalRecords), "SplittedData", Split( Last( FirstN( HTTPCollection, Value ) ).Result, "~" ) ) ); ForAll( TotalIterations, Collect( FinalHTTPCollection, { Title: Last( FirstN( Last( FirstN( TotalIterations.SplittedData, Value ) ).SplittedData, 1 ) ).Result, PinCode: Last( FirstN( Last( FirstN( TotalIterations.SplittedData, Value ) ).SplittedData, 2 ) ).Result } ) );
- In case, if you are getting more columns from the Power Automate, then you need to only update the code in ForAll block by adding the respective column names with their respective index of presence (highlighted piece)-
PinCode: Last( FirstN( Last( FirstN( TotalIterations.SplittedData, Value ) ).SplittedData, 2 )
).Result- With this, we have completed the READ part of this post series.
- So, this way, we can perform all CRUD operations using PowerApps, Power Automate, REST API, SharePoint Online-
- Create
- Read
- Update
- Delete
- Lastly, I would like to thanks Mr. Mariano Gomez for helping me when I got stuck while sending the data back to PowerApps because of using the wrong structure and also when I got stuck after splitting the data in PowerApps. You may also reach out to him on his YouTube video.
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.