How to Creating Code-Based Power Automate Flow
Image Source: Shutterstock.com
Power Automate
Nov 14, 2023 7:42 PM

How to Creating Code-Based Power Automate Flow

by HubSite 365 about David Wyatt

Senior Staff Engineer - Intelligent Automation Developer

External Blog Post
Citizen Developer

Power Automate

Discover how to create Power Automate Flow in code with Microsoft - Enhance your automation and editing processes with ease!

Within the realm of automation, a leading tool known as Power Automate, flaunts an intuitive user interface reminiscent of the Logic App UI, yet even more accessible, allowing virtually anyone the capability to craft remarkable automations.

The rationale for coding a workflow rather than utilizing Power Automate's user-friendly interface might elude many, however, the author, David Wyatt, embarked on this endeavor regardless.

Wyatt's intrigue in the automated workflow code sparked when faced with updating a staggering 500 workflows lacking connection references, a task that would otherwise demand a tedious, manual update of every single connection when deploying to an isolated environment.

 

By engaging with the workflow's definition.json file, he could pinpoint the crucial connections. This led to the creation of a workflow that modified the definition with updated connection references, simplifying the process significantly so that upon import, only three connection references were needed instead of 1500.

He found the direct editing of the definition file advantageous in several cases, especially for bulk find-and-replace tasks or validating complex expressions. However, the process entailed exporting, extracting from a zip file, editing, reinserting, and then re-importing— a series of steps that proved excessively cumbersome.

Then, a Chrome extension named Power Automate Tools was discovered, dramatically streamlining the process of live code editing within the browser.

 

With this tool, when a workflow is being edited, a simple click on the extension's icon opens up its definition for live edit and saves within the web browser. This utility also boasts Intellisense and a validation feature akin to that of the standard UI.

Subsequently, this enabled the author to code workflows more effectively. Initially crafting the workflows in the standard user interface, Wyatt then transitioned to coding after reviewing examples and interpreting the schema from Microsoft.Logic/schemas, eventually gaining proficiency in the creation of functional workflows.

  • The workflow's definition object encapsulates all actions or steps in an 'actions' node, consistent with container steps like Scopes and Conditions.
  • The elementary action structure includes references to the previous action it follows, metadata such as an 'operationMetadataId', its 'type', and 'inputs' that detail the information flowing from the action or step.
  • Additional details in the inputs section often cover connection information, user inputs parameters, and authentication which is generally consistent.

An example action, named 'Get_Date', succinctly demonstrates this structure, designed to operate subsequent to an action that lists all rows within an Excel table, with its input defined by the '@utcNow()' expression.

For those embarking on defining workflow objects, Wyatt recommends a handy website, www.objgen.com, which transforms plain text into JSON format, significantly optimizing his workflow despite requiring multiple windows and some data transferring.

While not crucial to the operation of workflows, keys like 'operationMetadataId' can be excluded freely, highlighting the shifting requirements and adaptations in this automation framework over time.

Despite these insights, the author advises against creating flows primarily through code. Although he managed to craft basic functioning flows, the limitations became apparent with more sophisticated requirements:

  • The UI's speed and intuitive handling of steps/actions outpace coding methods.
  • Auto-population of inputs, such as SharePoint site listings, leverages internal API calls not readily replicated in code.
  • Dropdown selectors in the UI, which conveniently mask complex GUIDs, present a significant challenge in manually tracking within code.
 

However, code-editing shines when applied to updates and validations. Where creating from scratch in code is arduous, making incremental modifications proves advantageous:

  • Find-and-replace for updates are executed with greater ease.
  • Validating or reviewing code becomes simpler through key searching, such as ensuring proper 'runAfter' sequences or pagination settings in actions.
  • Managing large or deeply nested flows can bog down the browser UI, whereas code edits maintain smooth performance.
 

While Wyatt had not intended to abandon the UI in favor of code, he frequently found himself switching between the two, utilizing the code as a supplemental tool far more than anticipated.

A special mention was given to Kamil Rithaler, developer of the Power Automate Tools extension, which, according to Wyatt, not only facilitated code editing but was also offered free of charge, enhancing the overall usability of this method.

 

Understanding Workflow Automation

Workflow automation is a transformative approach to streamlining business processes, minimizing human error, and enhancing overall efficiency. By automating repetitive tasks through software like Power Automate, organizations can redirect their focus towards more strategic initiatives that require human creativity and critical thinking.

It involves designing, executing, and automating processes based on workflow rules where human tasks, data, or files are routed between people or systems based on pre-defined business rules. With the advancements in cloud computing, automation tools are now more accessible to businesses of all sizes, promoting a culture of productivity and innovation.

Read the full article Creating a Power Automate Flow in Code

Power Automate - Step-by-Step Guide to Creating Code-Based Power Automate Flow

Learn about Creating a Power Automate Flow in Code

 

The blog offers insights into creating a Power Automation Flow in Code. The process can seem a bit counterintuitive, considering Power Automation's user-friendly UI, designed to allow anyone to create amazing automations. Yet, the blog details the reasoning and method for doing the same within code.

An initial spark of interest in code came about when the author needed to update 500 flows that lacked connection references. The process began by looking at the definition.json, identifying the flow connections, and creating a flow that read the definition. Subsequently, the flow replaced the connection reference and updated the json.

Despite having a tool for editing/creating in code, the author also flips between the UI and the code. By studying the examples and reading the schema Microsoft.Logic/schemas, they were able to create working flows quickly.

Understanding the basic structure of an action is essential. The action structure consists of three main aspects: runAfter, metadata, and type. An example would be a compose, named Get_id, which runs after an excel list all rows action, and has an input which is the utcNow() expression. Certain actions may have additional nodes in inputs like host- connection info, parameters- inputs from user and authentication. Container actions are similar, but have the actions node containing all internal actions/steps.

The blog mentions an invaluable tip regarding a useful website called www.objgen.com, which can convert simple text into json. Using the Peek Code in the UI allows to observe parts of the action/steps code.

RunAfter key dictates the flow order, while within containers, steps don't have a runAfter, as they use the containers. Some steps within the order of the keys may not matter, but with the user interface derived order isn't consistent. Also, not all keys are required. For instance, operationMetadataId can be removed without impact.

The author however, warns against creating flows in code. This is due to reasons like the speed of the UI, auto-populated inputs, handy expressions/variable list, and the simplicity of order. Still, editing in code proves incredibly useful for updates, validating or code reviews, and editing large/nested flows.

Regardless of the challenges, the author has found editing in code a useful tool and frequently toggles between the UI and the code review. A nod to Kamil Rithaler, the developer of Power Automate Tools is noteworthy as the tool significantly contributes to making code edits practical.

For further reading, the blog offers links to logic-apps-workflow-definition-language and workflow-definition-language-functions-reference.

  • Power Automation Interface
  • Understanding Code Structure
  • Key Tools & Resources for coding
  • Strengths and Limitations of coding in Power Automate

Keywords

Power Automate Flow, Code, Create, Automation, Microsoft, Workflow, Tutorial, Process Automation, Flow Construction, Flow Creation