How to trigger a Power Automate flow on a Dynamics 365 Many to Many (N:N) relationship

Currently there is no way to trigger a Power Automate flow from the addition or removal of a related record in a many-to-many relationship, natively within Power Automate.

You can however, use the Dynamics API to accomplish the same functionality.

Applies to:

  • Dynamics 365 On-Prem

  • Dynamics 365 On-line

  • Power Platform


Start Building the Power Automate Flow

Start with adding a Cloud Flow to your environment, selecting the Automated flow type.

Next, select the “When a HTTP Request is Received” as your trigger step.

Save the flow so that your URL is generated, then copy the URL to a notepad or similar application

Next, add a line break at each ‘&’ in the URL so that you can break out the URL Variables that you'll need in the next step.

Next replace the ‘%2F’ with a ‘/’
Remove any ampersands - ‘&’
Remove the questionmark from the end of the URL

Now you have the info you need to create a webhook

Add the Triggers

For this step, we’re going to need to jump through some hoops.


If you don’t have the Dynamics 365 Plugin Registration Tool, you can install it using the Powershell command.


Next, run the Plugin Registration tool

Connect to the Dynamics Environment

Select the Register button and select Register New Web Hook

Add the name ‘TriggerNtoN’
Copy and paste the URL from your TextPad/NotePad to the Endpoint URL
Select HTTPQueryString for the Authentication option

In the Keys/Values pairs list, select Add Property and add each item that you seperated out in the notepad earlier.

Click Save.

Next, Select the newly added Web Hook, click Register and select Register New Step

Complete the following:

Message: Associate
Primary Entity: none
Secondary Entity: none

The remaining fields should auto-fill themselves.

Click Update Step

This has now configured Dynamics to send a HTTP message to the Flow URL when it is triggered.

NOTE: If you want to also trigger when a record is removed from the many-to-many relationship you can add a second step, and select DISASSOCIATE for the Message

Finish your Power Automate Flow

Now that we have the event trigger added, any time it is triggered, it will send the message to the Power Automate flow and start the process.

Head back to your Power Automate; you can pull some info from the message to allow you to process additional steps.

Limiting the flow to one relationship

You probably don’t want the flow to fire every time ANY many-to-many records are related, so you’ll need to add a filter to the trigger.

In this example, we are limiting this flow to execute on the relationship Contact<>Expertise.

Head to your relationship setup in the Dataverse first and get the relationship name.

Next in the flow, select the ellipses and pick Settings on the trigger step

Add a trigger and update the code.

Replace <<RELATIONSHIP NAME HERE>> with your relationship name

@equals(triggerbody()?['InputParameters'][1]?['value']?['SchemaName'],'<<RELATIONSHIP NAME HERE>>')

Press Done and then you can start building out your flow.

Accessing your Table GUIDs

I’d recommend you add a ‘compose’ step and add the ‘body’ of the triggerbody to it so that you can see the code that is returned when run your process.

NOTE: With a Many-To-Many relationship you can add records from either end - or rather, you can attach from each side of the relationship. You will need to take this into account and process accordingly.

Inside the body of returned HTTP code, you should see three blocks of data; one will be the Target record GUIDs, one will be the relationship information and the third will be the Related Table record GUIDs.

Each can be accessed using a variation of the trigger body code above.

Target:

triggerbody()?['InputParameters'][0]?['value']?['Id']

Related Entities:

triggerbody()?['InputParameters'][2]?['value']?[0]?['Id']

Interested to learn more ?

Im gonna be honest - Dynamics 365 is one of those tools that is an incredibly powerful platform for your organization. With that power, comes a certain level of complexity. If you’d like to get some real answers about how Dynamics can benefit your organization, give me a call for a quick chat to see if it’s an option for you.

Give us a call at 604.846.4402 or email info@dominicsystems.com


Related Content:

Microsoft Plugin Registration Tool

Previous
Previous

What is Customer Voice for Dynamics 365?

Next
Next

How Can Microsoft Dynamics 365 Customer Service improve my customers experience?