Power Automate: Get HTTP Status Code and Output from a non-HTTP action

I came across the requirement today where I had a HTTP triggered Flow that was being called by another application. The Flow was making some insertions to Dataverse using the Add a new row Action. I needed to return the HTTP response (and error message if it failed) to the client application.

I am used to doing this for HTTP actions as you can easily select the Status Code, Headers and Body of a HTTP action from Dynamic content to place them in the corresponding properties of the Response action.

You can’t access those properties in Dynamic content for a Dataverse Action though, so to be able to access the Status Code, Headers and Body of the Dataverse Add a new row Action, I used the following Actions and Expressions.

Compose Response (Compose)
I created a Compose action which is configured to run after Success and Failure and contains the following expression to select the Output of the Add a new row Action:

actions('Add_a_new_row')?['outputs']

Successful Example
A trimmed down example output I received using the above expression in Compose Response for the Dataverse action is:

{
  "statusCode": 201,
  "headers": {
    "Cache-Control": "no-cache",
    "OData-Version": "4.0",
    "Public": "OPTIONS,GET,HEAD,POST",
    "Date": "Wed, 06 Sep 2023 04:33:25 GMT",
    "Allow": "OPTIONS,GET,HEAD,POST",
    "Content-Type": "application/json; odata.metadata=full",
    "Expires": "-1",
    "Content-Length": "7228"
  },
  "body": {
    "@odata.context": "https://yourtenant.crm.dynamics.com/api/data/v9.1/$metadata#ent_myentity/$entity",
    "@odata.type": "#Microsoft.Dynamics.CRM.ent_myentity",
    "@odata.id": "https://yourtenant.crm.dynamics.com/api/data/v9.1/ent_myentity(a1b2c3d4-1a2b-b3d4-e5f6g7h8)",
    "modifiedon@OData.Community.Display.V1.FormattedValue": "6/09/2023 4:33 AM",
    "modifiedon@odata.type": "#DateTimeOffset",
    "modifiedon": "2023-09-06T04:33:25Z",
    "statecode@OData.Community.Display.V1.FormattedValue": "Active",
    "statecode": 0,
    "statuscode@OData.Community.Display.V1.FormattedValue": "Active",
    "statuscode": 1
  }
}

Failure Example
A trimmed down example output received using the above expression in Compose Response for a failed Dataverse action is:

{
  "statusCode": 403,
  "headers": {
    "Cache-Control": "no-cache",
    "Allow": "OPTIONS,GET,HEAD,POST",
    "Content-Type": "application/json; odata.metadata=full",
    "Expires": "-1",
    "Content-Length": "1325"
  },
  "body": {
    "error": {
      "code": "0x80042f09",
      "message": "RetrievePrivilegeForUser: The user with id a1b2c3d4-1a2b-b3d4-e5f6g7h8 has not been assigned any roles. They need a role with the prvCreateent_myentity privilege."
    }
  }
}

Response (Response)
I then used the following expressions to access the Status Code, Headers and Body properties of the result of the Dataverse Action:

outputs('Compose_Response')['statusCode']
outputs('Compose_Response')['headers']
outputs('Compose_Response')['body']

I used the following expression to select the error message (if it was a failure so that I could pass it back to the client:

outputs('Compose_Response')?['body']?['error']?['message']

The Flow

Leave a Comment