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
(
    items('Apply_to_each')?['_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname'],
    's(',
    items('Apply_to_each')?['_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
  3. Hello Lin. Realise this is an old topic but perhaps you can advise how I can set a Row Owner in the Flow action directly. So, based upon the Condition checking the value in the Subject field of the new Row, I want to set the Owner value in the Row to be e.g. "John Smith". Can this be written directly into the Owner field using a formula?

    ReplyDelete
    Replies
    1. You can set the Owner value only if you have the systemuserid GUID value of the User, "John Smith". Here's the sample value (if the GUID of John Smith user is 39df6536.....)
      systemusers(39df6536-c9a9-4310-b3b7-9872ec39f1bd)

      Otherwise, you will have to use "List Rows" action and filter the user with the following filter and get the GUID
      (fullname eq 'John Smith')

      Delete
    2. Hello Linn. Many thanks for your response. Can you clarify where I use the formula to return the User details (I have the User GUID)? The flow has a Get a row by ID step, with the Row ID set to the users GUID. Later in the flow is the Lead Update action, where I want to change the Owner of the newly created Lead, based upon a separate value from a field within the Lead. What is the formula I would set in the Owner field of the Flow update action? I have currently got it set to: outputs('Get_a_row_by_ID')?['body/systemuserid'], this doesn't work.

      Many thanks again, Chris

      Delete
    3. Hi Chris

      What is the name of the field that stores the "a separate value from a field within the Lead"?

      E.g. if the name of the field is "Preferred User", you need to populate the Owner lookup as in the screenshot below with the value between the bracket of "systemusers()"
      https://www.screencast.com/t/kQ48ogC28

      Delete
    4. Hello Linn. Many thanks once again. The flow is managing new Leads created from a web portal. When created, the Lead record owner is set as "System". I want the flow to change the Lead record owner to a user for which I have the user guid. The decision as to which user is selected is based upon a condition that determines a "Subject" value, created in the web portal form. So, if Subject = X, the allocated the Lead record to "John Smith", of which the guid = 13afdba2-f7ca-eb11-bacc-0022481a7e8e.

      The Lead update action, to change the record owner, requires an expression in the Owner (Owners) field of the Lead, that points to the Get a row by ID flow step. I don't see any "systemusers" row, only "users", and there is no "Preferred User" option, only the "Unique user identity id", returned from the Get a row by ID flow step.

      The flow steps can be found below:
      https://screencast-o-matic.com/i/c3nDXQVb2NK
      https://screencast-o-matic.com/i/c3nDXjVb2NE
      https://screencast-o-matic.com/i/c3nDXiVb2Ng
      https://screencast-o-matic.com/i/c3nDXhVb2N0

      Delete
    5. Hello Linn
      Just to let you know that I have solved the User issue. "The "Get a Row by ID" step returned the User Guid as previously, I have now added a Compose action, "systemusers/outputs('Get_a_row_by_ID')?['body/systemusersid']", which returns the User name. Referencing the Compose step from the update of the Lead action, sets the correct user value.

      many thanks for your help.

      Chris

      Delete
    6. Hi Chris

      Sorry for the late reply. I am glad it was resolved.
      Technically, you should be able to assign the value for the Owner with the systemuserid from "Get a Row by ID" step as below without using a Compose step.
      https://www.screencast.com/t/EuUqR6Y4Vevc

      Delete

Post a Comment

Popular Posts