Dynamically Populate Owner Value (User or Team) in Power Automate

When building flows in Power Automate, sometimes we need more than just adding a dynamic value for complex scenarios (or at least for complex data types like polymorphic lookups). In this post, you will learn about how to set the value of the Owner field dynamically based on the value of another row's Owner field (which can be either User or Team).

Owner value from Trigger Output

In Dynamics 365/CDS, the row can be owned by either a user or a team. If we set the Owner column value based on the value of another row's Owner column using the Common Data Service (current environment) connector, it can be a bit tricky since we need to provide the EntitySetName (systemusers or teams) based on the value.

Example Scenario: Create a task for the Account row owner to verify when the Website of the Account is updated. The task needs to be assigned to the same owner as of the Account which can be a user or a team.

If we have to set the Owner column value from the triggering row (When a record is created/updated), we can use the flow expression below to handle both user and team table types.

UPDATED: Thanks Phil Cole for pointing out the conciser version of expression in the comment.

Expression with string concat
concat
(
    triggerOutputs()?['body/_ownerid_type'],
    '(',
    triggerOutputs()?['body/_ownerid_value'],
    ')'
)

Expression with If statement
if
(
	equals
	(
		triggerOutputs()?['body/_ownerid_type'],
		'systemusers'
	),
	concat
	(
		'systemusers(',
		triggerOutputs()?['body/_ownerid_value'],
		')'
	),
	concat
	(
		'teams(',
		triggerOutputs()?['body/_ownerid_value'],
		')'
	)
)
The format of the table type (logical name) properties of the lookup column in the trigger output is – _ownerid_type.

Owner value from Action Output

In the output of "Get a record", "List records", "Create a new record', "Update a record", and any other CDS (current environment) actions, the format of the lookup column is – _ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname.


If we have to set the Owner column value from the action row (Get a record), we can use the flow expression below.

Expression with string concat
concat
(
    outputs('Get_a_record')?['body/_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname'],
    's(',
    outputs('Get_a_record')?['body/_ownerid_value'],
    ')'
)

Expression with If statement
if
(
    equals
    (
        outputs('Get_a_record')?['body/_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname'],
        'systemuser'
    ),
    concat
    (
        'systemusers(',
        outputs('Get_a_record')?['body/_ownerid_value'],
        ')'
    ),
    concat
    (
        'teams(',
        outputs('Get_a_record')?['body/_ownerid_value'],
        ')'
    )
)

Based on the name/type of action, the expression needs to be changed accordingly. Take note that the item property of the "Apply_to_each" for array output (e.g. from List records action) does not require "body/" before the column name.
e.g.
  • items('Apply_to_each')?['_ownerid_value']
  • outputs('Create_a_new_record')?['body/_ownerid_value']
Expression for Apply to each
concat
(
    outputs('Get_a_record')?['body/_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname'],
    's(',
    outputs('Get_a_record')?['body/_ownerid_value'],
    ')'
)

Expression for Create a new record
concat
(
    outputs('Create_a_new_record')?['body/_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname'],
    's(',
    outputs('Create_a_new_record')?['body/_ownerid_value'],
    ')'
)

Summary

By using the "if" expression to check the table type (logical name) of the owner of the existing row, we can dynamically set the owner value of another row and specify the EntitySetName (systemusers or teams) dynamically in the expression.



Stay tuned for my next post where I will explain how to set the value of the Regarding field (or any polymorphic lookup field) dynamically based on the value of another record's Regarding field (which can be of any entity type).

Comments

  1. Very helpful. Thank you!

    ReplyDelete
  2. Hi Lin,

    Since the owner will always be set on record (aka row) I think the following formula will do the trick, thus eliminating the need for the if statement.

    concat('/', triggerOutputs()?['body']?['_ownerid_type'], '(', triggerOutputs()?['body/_ownerid_value'], ')')

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Phil - you are incorrect! Ignore the above!

      Delete
    3. Phil, you're right. I have updated the expressions to conciser version. Thanks for your feedback. 😊

      Delete

Post a comment

Popular posts