In this #PowerShot, I will show you how to trigger a flow in Power Automate when a record is created in an N:N relationship entity in CDS. Currently the CDS Current Environment and the CDS connector in Power Automate does not support the system created N:N relationship entity in the trigger actions. We will look at a workaround on how to do this using a WebHook.
Let's Get Started!
When an N:N relationship is created between two entities in CDS, it created another entity in the backend. Currently, the "When a record is created updated or deleted" trigger from the CDS Current Environment connector and the "When a record is created", "When a record is updated" triggers from the CDS connector do not support inputs as the relationship entity. For example, a relationship exists between the Users and the Teams entity and the entity name is "teammemberships". The relationship name is "teammembership_association". In this post, we will use this information and create a flow to send an email when a user is added to a team.
Power Automate
Step 1: Trigger- When an HTTP request is received: to trigger the flow from the WebHook. We will be creating the WebHook configuration in the next section.
Step 2: Trigger settings: Based on the data sensitivity, you can secure the inputs and outputs for the trigger and the data will be masked in the run history. This is not a mandatory step. The WebHook registration will trigger the flow everytime a record is created in any N:N relationship entity. As we just want to trigger this flow when a user is added to the team, we will add a trigger condition. Expression: @equals(triggerbody()?['InputParameters'][1]?['value']?['SchemaName'],'teammembership_association')
Step 3: Action- Get record: To get the user record from CDS. Expression used in item identifier: triggerBody()?['InputParameters'][0]?['value']?['Id']
Step 4: Action- Get record: To get the team record from CDS. Expression used in item identifier: triggerBody()?['InputParameters'][2]?['value'][0]['Id']
Step 5: Action- Send an email: To notify the user that they have been added to the team.
Note: We are triggering the flow based on the relationship name. Therefore, it will trigger even if the record is added other way round i.e., the user is added to the the team from the teams entity form. In such case, you might have to add a validation to verify that the user was added to the team from the users entity form.
WebHook
Step 1: Create a connection to the environment in which you want to trigger the flow in the Plugin Registration tool.
Step 2: Create a new WebHook Registration and provide the details as shown in the image below. The URL from the 'When an HTTP request is received' trigger of the flow created in the previous section needs to be split across query parameters and the authentication should be of type 'HttpQueryString' as shown in the screenshot below.
Step 3: Register a New Step for the WebHook. Provide all the details for individual parameters as shown in the screenshot below.
Note: The associate type step does not allow to select entities and so we have used the trigger conditions in the flow to trigger only when a specific relationship name appears when this WebHook calls the flow. (The relationship between Users and Teams entity in this example.)
Setup in Action
In this post we saw how to use a WebHook to trigger a flow in Power Automate when a member is added to a team in the CDS. Similar setup can be created for any N:N related entity and the flow trigger conditions can be modified to look for the particular relationship name only.
I hope you found this interesting and it helped you. Thank you for reading!
Comments