Power Automate: Make Your Flow Change Its Own Trigger Recurrence

Making the flow change it’s own trigger value based on the Discord OAuth token lifecycle!

Alright! Get ready for some recursive stuff! ๐Ÿ” In this one we’re going to make your own flow automatically update it’s own recurrence values, so that it can automate its own runtime schedules. Now I want you to follow along closely, because we’re going to involve a few actions that we will add and remove during development… Which is a bit important later on ๐Ÿ˜‚. First, let me introduce you to the “Power Automate Management” connectors – and more specifically the “Get flow” and the “Update flow” actions. Not many knows about its existence, but we can actually manipulate the flow application using it’s schema-structure. For some of the Logic Apps developers out there, this will be the same schema-structure as when you develop them in code view.

The specific demo I’m using for this tutorial is an expansion of my previous flow from blogpost – Power Automate: Store your API Access tokens in Azure Key Vaults and automatically renew them! – recursion.no

Keep in mind that even if it’s a specific demo, the techniques used can be applied to whichever flow you may use it with in the future. So let’s go!… ๐Ÿ˜Ž

Our goals:

  • Create a dynamic trigger for our flow based on it’s own value fetched from other external services.
  • Learn about how to store and backup your Power Automate schema in SharePoint
  • Learn how to manipulate your Power Automate schema to change it’s own trigger during flow runtime

Prerequisites:

  • A dedicated SharePoint List
  • Power Automate Premium license to use HTTP actions (Only for the Demo purpose, and not necessary to your specific needs)
  • A basic understanding of JSON and Logic Apps Schemas


Creating the SharePoint list

First of all, let’s get the prerequisites out of the way. Create a new SharePoint list and call it “Dynamic Flow Scheduler“. In that list, start with creating a single column representing your flow’s schema structure.

Navigate to your preferred SharePoint site and create a new list.
Select “Blank list” as a new list.
Name it “Dynamic Flow Scheduler” and click on “Create
When the list has been created, create a new row.
Name the title “Schema Definition” and click on “Save“.
To make it easier for you to follow through the demo, I want to show an existing column called “ID“. Click on “Add column” and “Show or hide columns“.
Mark the “ID” column and save by clicking on “Apply“.
Give the new Column the same name as your flow, in my case – I’ll call it “Update Discord Token

Creating your flow (Demo scenario)

For this particular demo purpose, I’m going to use the flow to refresh a Discord Token and set the flow’s trigger recurrence to the next expire_in value from the response payload.

Create a scheduled flow called “Update Discord Token”. Set it’s initial value to every 10000 seconds (or as long as it doesn’t interfer with our testing period).
I’ve also renamed the trigger to add more description to it.
Add a “Get item” from the start and target the List “Dynamic Flow Scheduler” that you created earlier on. This will later be used when manipulating the flow schema. Just ignore it’s function for now as there is no relevant value in it yet.

For this Demo, I’m also adding four Key Vault Secret values from my Azure Key Vault to authenticate my HTTP-calls. The first two are for the Discord authentication, the last two are for updating my Key Vault Secret in Azure with HTTP-calls. I’m also securing these values by turning on the “Secure Outputs” option settings.

You can read more details about how I’ve configured my Azure Key Vaults from my previous blog: Power Automate: Store your API Access tokens in Azure Key Vaults and automatically renew them! – recursion.no.

Note: These can be left out if you are not using the Azure Key Vault feature for client ID & client secret values.

Getting the OAuth Token together with the “Expire_in” key-value that I will use to change the trigger recurrence value.
Run the flow once and then copy the returned payload. Add a “Parse JSON” action and navigate to “Generate from sample” and paste in the payload as an example.
Now I add the authentication to my custom Azure Key Vault App Service update the Discord token value
Run the flow again and then copy the returned payload. Add a “Parse JSON” action and navigate to “Generate from sample” and paste in the payload as an example.
Adding the last HTTP-action to update the Key Vault Secret value.

Manipulating the Power Automate trigger schema

Add a new “Scope” action and add the following: A “Get Flow” action, a “Compose” action with “Flow Definition” as input, a “Terminate action“.

Add an “Update Flow” action outside of the scope

Flow Definition Code:

json(replace(outputs('Get_item')?['body/UpdateDiscordToken'],'"replaceInterval"',string(body('Parse_JSON_-_Get_OAuth_Token_for_Discord_access')?['expires_in'])))

Now run the flow once! We want to fetch the Power Automate Schema!

After the run, copy the flow schema from the “Compose” action details and paste it in your favorite IDE.
Navigate to the Trigger section of the Schema, and replace the key-value “interval” from 10000 to “replaceInterval” (including the quotes)
Scroll all the way down to the “Scope“-action and delete everything related to the scope section.
Change the “Update Flow” schema’s “runAfter” to “HTTP_-_Update_Azure_Key_Vault“, ensuring that the “Update Flow” action will run after “HTTP – Update Azure Key Vault” action instead of the actions that we just deleted.
Now paste the entire configured Schema to our Sharepoint list column for our Flow.
Delete the entire “Scope”-action before the first run

Congratulations! We’re now done configuring the recurrence manipulation!

All we need to do now is to run it!

Now navigate back to the “Edit” mode of the flow!

E voilร , amici miei! Your interval has changed for the next upcoming run! And the flow is still the same! ๐Ÿ˜Ž๐Ÿ™Œ

Summary

We’ve learned how to automate your Power Automate flow’s runtime schedule by making it automatically update its own recurrence values. Using the “Power Automate Management” connectors and specific actions like “Get Flow” and “Update flow,” you can manipulate the flow application using its schema. With a dedicated SharePoint list and a basic understanding of JSON and Logic Apps Schemas, you can create a dynamic trigger for your flow based on its own value fetched from other services. By using these features, you’ve also secretly learned how to easily backup your Power Automate flows in a SharePoint list in the future! Props to you for making it this far! ๐Ÿฅณ

Below is the entire flow in recap:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *