Thursday, July 29, 2021

Power Automate: CRUD Operations Using REST API - Part 6 (Add-Ons)

 Hello Friends,

Welcome back with another post on Power Automate. In last 5 posts, we were discussing about CRUD operations using REST API in Power Automate with PowerApps. There, we had learnt about Create, Read, Update and Delete the data in SharePoint. In this post we will learn about some Add-Ons that can be used in filtering / sorting / pagination the data during READ execution. Before start, I would request you to go through my all last 5 posts whose links are given below-

  1. Power Automate: CRUD Operations Using REST API - Part 1
  2. Power Automate: CRUD Operations Using REST API - Part 2
  3. Power Automate: CRUD Operations Using REST API - Part 3
  4. Power Automate: CRUD Operations Using REST API - Part 4
  5. Power Automate: CRUD Operations Using REST API - Part 5

Let's start-

  1. CREATE ADD-ON-
  2. In my post (Part 1), we were talking about the item creation using Power Automate with REST API. If you wish to return the ItemID, Custom message back to PowerApps, please follow below steps-
  3. After the Action "Send an HTTP request to SharePoint", we need to capture the Status Code and Item ID. For this, add new action and choose Compose. Rename it "Compose (Status Code)". (Renaming is optional just to improve reading of code).
  4. Click on Inputs box and write the formula "outputs('Send_an_HTTP_request_to_SharePoint')?['body']['statusCode']" in expression box.


  5. This way, the Compose Output will hold the status code of HTTP request.
  6. Now, initialize a new variable (intNewItemID) of Integer type to hold the Item ID of newly inserted record. We will send this ID back to PowerApps.


  7. Next step is to add a string variable (strResponseMessage) to hold the message that we will return back to PowerApps.


  8. Now we will check the value of status code. For this, we will add a Condition and check if the value is 200 or 201. Details of HTTP REST API codes can be checked here HTTP Status Codes. If the condition matches, we will read the ID parameter from REST API response and set it to intNewItemID. Also, we will set our custome message to strResponseMessage.


  9. To set the value of intNewItemID, add an action "Set Variable" and in the Name box, choose the intNewItemID variable. In Value box, write the code "outputs('Send_an_HTTP_request_to_SharePoint')?['body']?['d']?['ID']".


  10. Once done, please save the flow and close it. After closing, reopen the flow. By doing so, the Value part written above get managed according the the appropriate syntax for flow. You may see the change in below screen shot (given for next variable).
  11. Now we will set the Response text. For this, add a new action "Set Variable". Choose the variable "strResponseText" in the name box and in value field, write your custom message. I have take the message as "Record created successfully.".


  12. For "No" condition, update the message.


  13. Now, at last, add the action "Respond to a PowerApp or flow" to send these values back to PowerApps.
  14. Here click on "+ Add an output" and then click on Text type. Write the title as "ID" and code "string(variables('intNewItemID'))" in expression box as response value and click on OK. We are sending the ItemId as string value back to PowerApps. You may send in numeric format also.
  15. Again click on "+ Add an output" and then click on Text type. Write the title as "Message" and select "strResponseMessage" variable as response value. 

  16. Save the flow and use the same in PowerApps.
  17. In PowerApps, store the output of Power Automate in a variable. Then extract each value separately from this variable. For this the code is-
  18. Set(
        PowerAutomateResponse,
        'HTTPCRUD-CreateStatusCode'.Run(
            "Sachin Jain",
            "234789"
        )
    );
    UpdateContext({ItemID: PowerAutomateResponse.id});
    UpdateContext({ResponseMessage: PowerAutomateResponse.message});
    


  19. This way, you can get the content from response. I had shown the output using Notify as below-


  20. You can check the response using Monitor.

  21. You can open Monitor using App Checker-

  22. This way you can send response back to PowerApps. The same way you can modify the UPDATE / DELETE part of flow to response back to PowerApps.
  23. READ ADD-ON-
  24. In my last post (Part 5), we were talking about getting the data from Power Automate using REST API. Till now, we haven't provided any filter. In case, if we have to get the filtered data then we can modify the Uri in "HTTPCRUD-Read" as below-
    1. Current- _api/web/lists/getbytitle('ListForRESTAPI')/items
    2. After applying Filter- _api/web/lists/getbytitle('ListForRESTAPI')/items?$filter=Title eq 'Sachin Jain'
  25. After applying this filter, we get the filtered data in PowerApps as below-


  26. You can pass this filter query from PowerApps itself. For this, you need to add an input parameter in Power Automate (say - ComposeFilterQuery_Inputs) and when clicking on get data, you have to pass your filter query from PowerApps to Power Automate. It's your wish that you send the complete filter query (?$filter=Title eq 'Sachin Jain') or just the query part (Title eq 'Sachin Jain').
  27. Remember, any change in Power Automate (adding / updating / deleting any input / output parameter), you need to add that Power Automate again in PowerApps code by clicking Action >> Power Automate >> PowerAutomateName. But this action has a drawback. The moment you add any Power Automate in code, it remove all the existing code from formula box. So make sure to copy the existing code in notepad and then add the updated Power Automate. Once added, replace the added Power Automate code line with the copied code. Now make changes in your code. Actually, when you click on Action >> Power Automate >> PowerAutomateName, the system adds the latest schema of that flow in system. Once the schema gets loaded, then you can just update the code by writing it manually.
  28. In both cases, the Power Automate will look like-


  29. Same way, we can apply sorting also. For this, add "?$orderby=Title" code at the end of Uri.


  30. The output is:


  31. This way, you can add more functionalities to CRUD operations using Power Automate and REST API.
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.