Working on Lookup Field of Dynamics 365 in PowerApps

By | July 2, 2018

Introduction:

PowerApps is a quick service that helps you to easily build business apps that runs on browsers and in phone or tablet without any coding experience needed. It allows anyone in an organization unlock new business agility.

Recently we had a business requirement, where we need to work on lookup field of Dynamics 365 using PowerApps, but currently, we cannot directly fetch the lookup values in PowerApps. So after some research and play around we found a solution on this.

This blog explains about steps to read and set Lookup fields of Dynamics 365 in Power Apps.

1. Create a new power Apps by selecting the Phone Layout as shown in the below screenshot.

Working on Lookup Field of Dynamics 365 in PowerApps

2. Select the database i.e. Entity and then click on Connect as shown in the below screenshot.

Working on Lookup Field of Dynamics 365 in PowerApps

3. We get the following three screen on the left side

  • Browse Screen
  • Detail Screen
  • Edit Screen

4. Click on Browse Screen and user can see the List of Opportunities in the CRM.

5. Now as seen in the below screenshot user can see GUID of parent account.

Working on Lookup Field of Dynamics 365 in PowerApps

To display the name of the parent account follow the steps as given below:

  • Add a data source of Account i.e. Account Entity by selecting on View, Data source and then select “Add Data Source” as shown in the below screenshot.

Working on Lookup Field of Dynamics 365 in PowerApps

To display the name of the parent account set the text property of the Body1 which contains parentaccountid as below:

LookUp(Accounts,accountid=ThisItem._parentaccountid_value,name)

The Lookup Property search the accountid in Accounts which equals to GUID shown in textbox and returns the name of the Account.

Working on Lookup Field of Dynamics 365 in PowerApps

Now you can see the name of the parent Account which is selected on Opportunity as shown below.

Working on Lookup Field of Dynamics 365 in PowerApps

Following are the steps to assign Lookup value in PowerApps:

1. Click on Edit Screen and insert the “Search” Icon inside a Data Card as shown in the below screenshot.

Working on Lookup Field of Dynamics 365 in PowerApps

2. Insert another List screen and add data source as Accounts. This screen will show list of Accounts present in CRM.

Working on Lookup Field of Dynamics 365 in PowerApps

 

3. On “OnSelect” property of Search Icon set the formula as:

Navigate(AccountScreen,ScreenTransition.Fade,{searchAccountName : DataCardValue12 .Text});Clear(accountToBeAdded)

Working on Lookup Field of Dynamics 365 in PowerApps

The above formula will navigate the Account Screen which shows list of Accounts and update the value of searchAccountName by value in DataCardValue12

4. Now on Account Screen, on “OnSelect” property of  icon write the formula as

ClearCollect(accountTobeAdded , {account :BrowseGallery2.Selected});Back()

Working on Lookup Field of Dynamics 365 in PowerApps

This formula create a collection name as “accountToBeAdded” and add the selected account in Collection and navigate back to the Edit Screen.

5. Now go back to the edit form, on text box of the Account  set the Default property  as following:

If(IsBlank(First(accountToBeAdded).account.accountid),Blank(),First(accountToBeAdded).account.name)

Working on Lookup Field of Dynamics 365 in PowerApps

This formula check whether the “accountToBeAdded” collection contain accountid. If true set the name else set it as “Blank”.

6. On Update property of Data Card set the formula as:

First(accountToBeAdded).account.accountid

Working on Lookup Field of Dynamics 365 in PowerApps

The above formula will update the Data card value to the selected account.

Conclusion:

Using the simple steps explained in this blog user can read and set Lookup fields of Dynamics 365 in Power Apps.

We would like to take a moment to introduce our new product Alerts4Dynamics to you. Alerts4Dynamics lets you schedule and manage alerts in Dynamics 365 CRM to notify users about the updates in CRM, due invoices, reminder to send quotes, etc. You can define target audience and send them priority based alerts via pop-ups, form notifications and emails. You can also view the log of read/dismissed alerts by users and also create alerts for multiple records by defining rules.

14 thoughts on “Working on Lookup Field of Dynamics 365 in PowerApps

  1. Dave

    I’ve completed this work-around, but ran into one issue. I can’t seem to make the DataCard it belongs to Valid as a required field. The validity of the field doesn’t seem to realize there is a value so it stays Invalid. Is there a way to make this work?

    Reply
    1. Inogic Post author

      Power App does not enforce field to set as required as shown in dynamics365.

      1.To make the fields required we can set a the Onselect property of Submit icon as:

      If(!IsBlank(DataCardValue18.Text),SubmitForm(EditForm1),UpdateContext({ShowMessage: true}))

      The above formula will validate if DataCard contains value, if true will submit the record else will update the ShowMessage variable to true.

      Please see the screenshot below:

      2.Now, add a label to show the error message and set the visible property of label as ShowMessage.

      3.Now if the DataCard does not contains value, error message will be displayed.

      Please let us know if we are missing something so that we can assist you further.

      Thanks!

      Reply
  2. Jeff Zimmerman

    I’m stuck on Step 4. Could you please help?

    In my case, instead of Accounts I’m working with a custom entity called Districts.

    I got Step 3 to work (it gave me an error until I did Step 4, because Step 4 initializes the “districtToBeAdded” collection).

    But Step 4 produces another “Name isn’t valid” error. Is it because of the “{district : DistrictList.Selected}” context? Do I need to initialize something called “district” first?

    Here’s my attempt at step 4: https://case-my.sharepoint.com/:i:/g/personal/jzimmerman_casedupage_com/EVsv0OZhhnlFuJgfAaZ7roQBS2i57Ej2nxmutZ6bDjgeRg?e=2OLgez

    Reply
    1. Inogic Post author

      No, you do not need to initialize district first to store the values in the collection because all variables are initialized implicitly when declaring in Collection.

      You can refer below link to understand more on variables:

      https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/working-with-variables

      The formula to store the value in collection is below:

      ClearCollect(districtToBeAdded, { district : BrowseGallery2.Selected });

      Make sure you are selecting a record from the gallery and not from the card to add the record in the collection.

      Regarding Error “Name isn’t valid”: Every time you create a new collection in Power App the name of the collection must be unique.

      Hope this helps!

      Thanks!

      Reply
  3. Jefferson Daniel

    Im stuck on the last part step 6. I’m working with User entity to assigned stuff. Search icon goes to the UserScreen. Selects Users Just fine. Stores Users in the variable & collections just fine. Get to step 6. Tried systemuser.systemuserid which should be the equivalent id for system user entity vs accounts.

    When i use this First(userToBeAdded).systemuser.systemuserid on a textinput field. It works fine. I tested 2 text input fields to verify the search icon was working. Shows the user and user id. When i use it on a DataCard as you instructed. I keep getting this error “The property expects Record values, but this rule produces incompatible Guid values”. If i ignore step 6 the app performs everything right except saving the values properly. So assuming i need step 6 to finish this off.

    Reply
    1. inogic Post author

      We are unable to replicate such issue. Please, can you provide us exact steps on this issue?

      Let us know how you are updating record in CRM using Patch function or Submit Form function in Power App.

      Thanks!

      Reply
  4. Derek

    I have Dynamics 365 as the data source and I have a lookup field in D365. In my PowerApps edit form when I add the lookup field as a dropdown it shows up but is pointing at the wrong data. I can change the data to point at the correct things and the lookup field works as I want it too. However the lookup field does not save correctly. Is it somehow possible to save this lookup field as it functions just as i want it to?

    Thanks

    Reply
    1. inogic Post author

      Hi,

      As per step 6 in our blog make sure you update the correct field name of the lookup.

      And as per your question, yes we can update lookup field in CRM using patch function.

      You can refer below link to understand more on patch function:

      https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-patch

      Simply filter the record by patch request and update the lookup field by setting fields “type” and “value” of the lookup field.

      Example: _parentaccountid_value: Set the GUID of the record.
      _parentaccountid_type: Set the entity name in string.

      For more information you can refer the blog as below:

      https://www.inogic.com/blog/2018/07/create-attachment-in-crm-with-camera-control-using-canvas-app-in-powerapps/

      Hope this helps!

      Thanks!

      Reply
  5. Lee

    Hi,
    I have followed the step to work with my App. I am fine with the first 5 steps. However, there is no update function as my one is a custom entity.

    When I create or edit a record, I can’t save or update the value in the lookup filed. I have read the Patch function, as there are 3 custom lookup filed in my EditForm, and I need to deal with non-fixed records smartly rather than mentioning the name of a specific record.

    Please advise how can I achieve my goal.

    Thank you,
    Lee

    Reply
    1. inogic Post author

      Hi,

      Please follow below steps to save the value in the lookup field:

      1. On default property of textbox write the formula as “First(accountTobeAdded).account.’Account Name’” as shown in below screenshot:
      PowerApps
      2. Now insert a label below the textbox and add the text property as “First(accountTobeAdded).account.Account” as we need to store GUID of the record for later Patch request to create/update record.

      Please see the screenshot below:
      PowerApps
      3. Now while using Patch function to create/update the record, add value to the field as GUID(label3.text)

      For Example: storename: GUID(Label3.Text),

      FYI, later you can hide the label from the form by setting the visible property as false.

      Let us know if you have any queries regarding the same.

      Hope this helps!

      Reply
  6. Lee

    Hi,

    Thank you for your timely reply. I have followed your advise, however, I still cannot update the data.
    I have tried both ways in {Division:DataCardValue4} and {Division:Label1}.

    Sorry I cannot attached a screenshot here directly. Please check my codes in the following link.
    The Screenshot of my App: https://imgur.com/a/9TankCi

    Thank you again and looking forward your reply,
    Lee

    Reply
  7. inogic Post author

    Hi,

    In the first screenshot you provided as below:

    lookup-field

    Update “{Division: DataCardValue4}” as “{Division: GUID(Label1.Text)}” to update the lookup field.

    Hope this helps.

    Thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *