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.

Export Dynamics CRM Reports

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

  1. jzimmerman@casedupage.com'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
  2. drodriquez@arcticit.com'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

Leave a Reply

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