Get Power Automate Context using flow run Table with Microsoft API In MS CRM

Hi Everyone, After long time i am writing one of major set back to Handel the event from power automate context and get the record id and Trigger entity data using Microsoft API.

                       For Every MS CRM Project mostly they are implementing the power automate to run the Custom user logic. If cloud flows are more in tenant, if user ask to some logic was not worked for the one of the record, in that case, we need to open our cloud flow and go to all runs get the flow run for specific time period. It will take more time if run was there more then 1 week or 1 month, in That case we need to handle that scenario, without opening the Power automate we need to get the flow run URL for specific record. So now we came with the best approach to handle these type of Issue for Developers.

                     Main point to get the logs for your tenant, DLP Policy will be enable to restrict accessing this flow Api, So please enable that before proceeding to implement this requirement. I am trying with Trail so there is no restriction to access the flow API connection.

Before going to post, we will discuss basic requirement to get the data. 

  1. For your environment, you must have Power automate licensee
  2. you must have system Administrator for Tenant to Access the flow run Table data
  3. You have access for power platform admin centre to enable the flow run logs to tenant ( This is optional, form request we can enable this feature)
If You have all , then you can easily get the data for all flow runs data

                       Before going to setup the Flow, first enable the below feature from platform admin centre
Power App--> Settings --> Admin Centre --> In Left navigation, go to Manage and open your environment --> Click on setting on Home ribbon and follow below Images steps to check  flow runs logs are enabled or not to  your tenant.
 
                         If above enabled in tenant we can go for implementation of logs access.


                          In my requirement I am accessing flow runs for past 2 hours and running this flow to schedule for every 2 hours, so I can get the latest runs and get the data


                         From Above list rows it will get the flow run records for past 2hours, while getting the fetch XMl, fetch XMl Contains flowrunid and  workflow Attribute to get the Flow Id and run id.
Below image is sample flow run id

                                             After getting list rows of flow run records, Using Apply to each loop the all flow runs. Inside Apply to each Add below Step to get the authentication to get the flow runs for flow.




 For Above step Base url is https://Environmentid.d4.environment.api.powerplatform.com

Microsoft Entra ID Resource URI is https://service.flow.microsoft.com/ This is fixed for all tenants.


 
After sign in it will create connection and  it will come like below
You should use the get method and Url of the request for:
 like
https://Environmentid.d4.environment.api.powerplatform.com/powerautomate/flows/Workflowid/runs?api-version=1

Here workflow id is from List rows of flow runs.

Use Parse Json step to get our required flow runs for the specific flow.
we can use sample Json below, or if you want to create sample Json run the flow once and it will generate output and from that output we can generate sample Json, In either way it will generate sample Json like below only
{
    "type""object",
    "properties": {
        "value": {
            "type""array",
            "items": {
                "type""object",
                "properties": {
                    "name": {
                        "type""string"
                    },
                    "id": {
                        "type""string"
                    },
                    "type": {
                        "type""string"
                    },
                    "properties": {
                        "type""object",
                        "properties": {
                            "startTime": {
                                "type""string"
                            },
                            "status": {
                                "type""string"
                            },
                            "correlation": {
                                "type""object",
                                "properties": {
                                    "clientTrackingId": {
                                        "type""string"
                                    }
                                }
                            },
                            "trigger": {
                                "type""object",
                                "properties": {
                                    "name": {
                                        "type""string"
                                    },
                                    "inputsLink": {
                                        "type""object",
                                        "properties": {
                                            "uri": {
                                                "type""string"
                                            },
                                            "contentVersion": {
                                                "type""string"
                                            },
                                            "contentSize": {
                                                "type""integer"
                                            },
                                            "contentHash": {
                                                "type""object",
                                                "properties": {
                                                    "algorithm": {
                                                        "type""string"
                                                    },
                                                    "value": {
                                                        "type""string"
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "outputsLink": {
                                        "type""object",
                                        "properties": {
                                            "uri": {
                                                "type""string"
                                            },
                                            "contentVersion": {
                                                "type""string"
                                            },
                                            "contentSize": {
                                                "type""integer"
                                            },
                                            "contentHash": {
                                                "type""object",
                                                "properties": {
                                                    "algorithm": {
                                                        "type""string"
                                                    },
                                                    "value": {
                                                        "type""string"
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "startTime": {
                                        "type""string"
                                    },
                                    "endTime": {
                                        "type""string"
                                    },
                                    "originHistoryName": {
                                        "type""string"
                                    },
                                    "sourceHistoryName": {
                                        "type""string"
                                    },
                                    "correlation": {
                                        "type""object",
                                        "properties": {
                                            "clientTrackingId": {
                                                "type""string"
                                            },
                                            "clientKeywords": {
                                                "type""array",
                                                "items": {
                                                    "type""string"
                                                }
                                            }
                                        }
                                    },
                                    "status": {
                                        "type""string"
                                    }
                                }
                            },
                            "isAborted": {
                                "type""boolean"
                            }
                        }
                    },
                    "isMigrationSource": {
                        "type""boolean"
                    }
                },
                "required": [
                    "name",
                    "id",
                    "type",
                    "properties",
                    "isMigrationSource"
                ]
            }
        }
    }
}

From Above Invoke An HTTP Request Step, it will generate Latest Maximum 50 runs for the given flow. Instead of looping all 50 runs, filter runs with List rows run id (Name Field in Flow run entity), from that we can get the context of the flow
Add step Filter Array and Add parse json Value for filter 



"item()?['name']" we should add this variable to filter run id from our HTTP Output

From this filter array output it will return 1 flow run data, we can use apply to each or first (output()) function to get the Data, in my case i am using Apply to each.
From Filter Array output we will get the ['outputsLink']?['uri'], we need to pass this parameter to get the trigger Context of the Power automate flow





Pass the body from filter array, it will return one value only.
After saving the flow and run manually, our flow will sucess

From HTTP Body we can see the trigger record id and entire context of the record 

From this way we can get the flow run context. By using this we can create event logs and pass the flow run id. In fuchure if anything is not worked, we can directly search with the Guid of the record and we can get the flow run for specific record, without opening the flow.

Limitation for this Apporach:

We will get the Output from HTTP Request if Flow runs for Data verse table only, if No data verse then it will return null.



Thanks for reading and  visiting my blog, Follow the blog.





Comments

Popular posts from this blog

Auto Number Id Generation for Any Entity using Plugin

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form