How to get (almost) all Flow runs

Posted by

The beauty of Power Platform is that with the access to Power Automate APIs (almost) anyone can use them and enhance the functionality.

In my case I needed to get Power Automate flow run history. It’s available via the call to “https://management.azure.com/providers/Microsoft.ProcessSimple/scopes/admin/environments/${environment}/flows/${flow}/runs?api-version=2016-11-01“, where the variables are your environment and flow ids respectively. Of course you need to have OAuth 2 token passed as Bearer in Authorization header (you know how to do it in Postman, yeah?)

Unfortunately, this call it returns only first 50 records. Fortunately though, it also return as a part of response a “nextLink” property which is just a ready to execute call to the next page of 50 runs! Everything you need to do is just to iterate again and again until you get all of them or (I suggest) hit certain fixed limit.

If it helps, here is extract from my working tool code in TypeScript. Feel free to use!

See gist here: https://gist.github.com/andrew-grischenko/a2d42fd707632c0928e72047fb7741fd

...
    let responseMessage: string = '';
    const statusOptions = [ 'All', 'Running', 'Failed', 'Cancelled', 'Succeeded' ];
    const NO_FILTER = 0;

    var runsList = {
        runs: []
    };
    let runsCount = 0;

    let nextLink: string = `https://management.azure.com/providers/Microsoft.ProcessSimple/scopes/admin/environments/${environment}/flows/${flow}/runs?api-version=2016-11-01`;
    if(statusOption != statusOptions[NO_FILTER])
        nextLink += `&$filter=Status%20eq%20%27${statusOption.toLowerCase()}%27`;
        
    try {
        while(nextLink && runsCount <= RUNS_LIMIT) {
            await getHttp(nextLink,
                req.headers['authorization'])
                    .then(runs => {
                        if(!runs || !runs.value)
                            throw Error('No runs found - the response is unexpected');

                        const items = (periodOption != periodOptions[NO_FILTER]) ?
                            runs.value.filter( (item) => new Date(item.properties.startTime) >= dateFrom && new Date(item.properties.startTime) <= dateTo) :
                            runs.value;

                        items.forEach(element => {
                            runsList.runs.push(
                                { 
                                    'id': element.id,
                                    'name': element.name,
                                    'type': element.type,
                                    'properties': element.properties 
                                }
                            )
                        });
                        runsCount += items.length;
                        nextLink = runs.nextLink; 
                });
        }
        responseMessage = JSON.stringify(runsList);
        context.log(`Total runs found: ${runsList.runs.length}`);
    } 
    catch ( err ) {
        responseMessage = `Error: ${err}`;
    }
    ...

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s