Sunday, October 15, 2017

PowerApps and CRM optionsets

In this blog, I will walk you through the steps on how to work with the Dynamics CRM option sets in PowerApps. I will be extending the app I built in my last blog CRM and PowerApps revisited.

Problem Statement

The problem is that if you use out of the box Dynamics 365 Connector for PowerApps, it only retrieves the numeric value of the optionset. It does not return the text value of the optionsets. Below, I have added a Preferred Method of Contact field to the DetailScreen1.
2017-10-14_22-57-42
As shown in the screenshot above, Preferred Method of Contact of 1 means nothing to a business user. It should display email, phone, mail, etc.

Solution

The Dynamics CRM connector for PowerApps does not display the text values of the optionsets or the metadata of the entities. So, there is no straight forward solution for this problem, but we can use something similar to a data maps table that contains what these numeric values mean.

Here are the steps:

Create a collection

We will create a collection (https://powerapps.microsoft.com/en-us/tutorials/create-update-collection/) to store the optionset values and the corresponding text.
1. Open the BrowseScreen1, choose On Start from the Actions Menu and add the following code.
Collect(Myc, {optionset:"PreferedContact", OptionValue:1,DisplayText:"Any"},{optionset:"PreferedContact", OptionValue:2,DisplayText:"Email"},{optionset:"PreferedContact", OptionValue:3,DisplayText:"Phone"},{optionset:"PreferedContact", OptionValue:3,DisplayText:"Fax"},{optionset:"PreferedContact", OptionValue:3,DisplayText:"Mail"})
2017-10-14_23-23-02
The above code will create a collection Myc.
  • DisplayText- displays the text of the optionset
  • OptionValue- displays the numeric value of the optionset
  • Optionset- displays the name of the optionset field. This field will be useful to the store the values for the multiple optionset fields in the same collection.

    2017-10-14_23-37-28

    Display the Optionset Text

    We will be using the PowerApps Lookup function to display the text for the optionset.
    1. Open the DetailScreen1 and select the optionset field, as shown on the screen in the screenshot below.

      2017-10-14_23-57-52
    2. Click on the Unlock the change properties.
    3. Change the Data property to LookUp(Myc,((OptionValue=preferredcontactmethodcode)&&(optionset="PreferedContact")),DisplayText).
    4. The DetailScreen1 will display the text associated with the optionset numeric value.

      2017-10-15_21-56-13

      Editing the Optionsets in PowerApps

      The above solution can show the text, but won’t let the user edit the values in the app. The solution to allow users to edit the optionsets values in PowerApps is little tricky.
      Here are the steps to achieve this.
      1. Add the Preferred Method of Contact optionset to EditScreen1.
      2. Change the display for the field to Allowed Values as shown in the screenshot below. It will change the field to a dropdown field.

        2017-10-15_22-12-51
      3. Click on the Unlock the change properties.
      4. Select the dropdown field and change the Items property to collection Myc. In the dropdown properties pane, select the column to display in the Value field. By default, the first column of the collection is selected.

        2017-10-15_22-30-50
      5. Change the Default property to LookUp(Myc,((OptionValue=preferredcontactmethodcode)&&(optionset="PreferedContact")),DisplayText).
      6. Select the optionset card and change the Update property to DataCardValue4.Selected.OptionValue. This will tell the card to use the OptionValue field of the collection to update in CRM.

        2017-10-15_22-46-06
      7. Save the application and test it.
      I hope this helps.

      Wednesday, October 11, 2017

      CRM and PowerApps revisited

      Last year in June, I had written a blog about PowerApps. I created a simple app that could update a few fields and the entity image of Contact records, but there were a few problems with the functionality. Microsoft Flow, CDS and PowerApps are getting a lot of attention these days, so I decided to revisit my original blog and create the same app with latest PowerApps functionality. Since the PowerApps studio has changed from the original version, I have a made a few changes to the app as well. I am using PowerApps app installed on my Windows 10 machine.

      Here are the steps:

      Creating an app using Dynamics 365 as data source


      Create connection and generate a default app

      1. In PowerApps, Select New  >> Start with your data” >> Dynamics 365 , as shown in the screenshot below.

        2017-10-08_21-42-12
      2. Create a new connection to your CRM Online organisation. In my case, I already have connections, as shown in the screenshot. Click on the dataset for your organisation (in my case it is HPE).

        2017-10-08_21-44-03
      3. Select Contacts from the dropdown and click Connect, as shown in the screenshot.

        2017-10-08_21-44-44
      4. PowerApps will create an app. The app will have 3 Screens:
        • BrowseScreen1 (Home Page with a list of the contacts and a search box)
        • DetailScreen1 (Displays the details of the selected item on BrowseScreen1)
        • EditScreen1 (Opens the selected record/new record in edit mode)

      Update search and sort conditions on the browse screen

      1. On BrowseScreen1, select the browse gallery. the editor will display the Items properties.

        2017-10-10_23-20-45
      2. Change the value of the “Items” property to SortByColumns(Search(Contacts, TextSearchBox1.Text, "fullname"), "lastname", If(SortDescending1, Descending, Ascending)). It will change the search criteria to “fullname” and sort the records by “lastname”.

      Select fields to display in the gallery

      1. Select the fields in the first item of the gallery and choose the CRM field you would like to display in that control, as shown in the screenshot below.

        2017-10-10_23-35-12
      2. Choose the other fields to display in the gallery. The image control on the gallery is already set to display the entityimage of the contact.

        2017-10-10_23-43-13

      Select the fields on Detail and Edit screen

      1. Select DetailScreen1 and then select Data sources from the View Menu. It will display the Data window as shown in the screenshot below.

        2017-10-11_00-00-48
      2. Select DetailForm1. It will display all the available fields in the Data pane as shown in the screenshot. Click on the purple eye in the options pane to add or remove the fields on the form.

        2017-10-11_00-06-16

      Change the theme of the app

      1. You can change the color schema for the app by selecting predefined themes under Home>>Themes.

        2017-10-11_10-17-21

      Save and Share the app

      1. Save the app by providing the name of the app. You have an option to save the app on the cloud or on the local machine.

        2017-10-11_00-17-27
      2. Share your app with your colleagues by selecting Share this app.

        2017-10-11_10-22-40

      Testing the App

      I am testing this app on iPhone 7S. To run PowerApps on your mobile device, install PowerApps from the app store.
      1. Logon to the PowerApps and choose your app.

        2017-10-11_11-27-56
      2. The app will display the browse screen.

        2017-10-11_11-30-11
      3. Select the record to view the detail screen. All the fields are disabled on this screen.

        2017-10-11_11-32-33
      4. Click on the edit icon to edit the record.

        2017-10-11_11-33-11
      5. Click on Change Picture to upload a picture. On iPhone, you will get the following options.

        2017-10-11_11-34-15
      6. Take a new picture/select an existing picture and press save.

        2017-10-11_11-35-30
      7. Have a look at the browse screen. It should display a picture on the edited record.

        2017-10-11_11-36-38

      Conclusion

      In general, I think PowerApps is great to build something quick instead of spending months to go live, but performance must be improved to make it usable in the real world.

      Improvements

      There are a few improvements in the latest version of PowerApps.
      1. Image control can display CRM entity images. This was not working a year ago.
      2. Image control can access iPhone camera. In my last attempt, I had to add the camera control to take the picture.
      3. The performance is better than a year ago.

      Issues

      1. Photos take a long time to load. On the browser screen, the data fields load very quickly but photos appear after a few minutes.

      2. Users can’t control the orientation of the photos selected in the image control. Some photos get loaded with the wrong orientation. I have read on the PowerApps community forum that the PowerApps team is working on it.

      3. CRM connector does not have an option to retrieve specific columns instead of all the columns.

      Sunday, October 8, 2017

      CRM Web API and Certificates Issues

      I have not written a blog in last 8 months as I have been travelling nonstop for work. In this blog, I am going to share some behavior I came across a few weeks ago.

      So here is the story. We are using CRM Web API for integration between the on-premise system and CRM online. We are calling the CRM Web API from JavaScript running on the on-premise server using OAuth authentication. We used a process very similar to what is described in the following article: https://msdn.microsoft.com/en-us/library/mt595797.aspx.

      Generally, we don’t need to manually install SSL certificates of public sites on our personal machines, as most browsers and operating systems have already installed the root CA certificate of well known certificate authorities like Symantec, Digicert, Microsoft, etc.

      For security reasons, in an enterprise environment most of the root CA certificates/trusted public authorities are removed from the integration servers.

      To make the successful calls to CRM Web APIs, you have to install the following certificates onto your integration severs.
      1. Certificate to communicate with https://login.microsoft.com
      2. Dynamics CRM 365 Certificate
      Now the real story. We installed the certificates, and everything was working as expected for a couple of months, when suddenly it stopped working. After some investigation, we found out that we are not getting the authentication token from https://login.microsoft.com. A bit more investigation, and we found out that the issue is related to the certificate. The server certificate presented by https://login.microsoft.com was different than the certificate we installed on our integration server. The old certificate was not expired, but still replaced by a new one. We replaced the certificates and everything is back to normal. I have attached the screen shots of both the certificates.

      Old Certificate

      2017-10-09_11-53-51

      New Certificate

      2017-10-09_11-55-44

      If you look at the certificates, the original certificate was issued by Symantec and was expiring on March 18th 2018, while the new certificate is issued by Microsoft itself. I hope this won’t happen again. So if your calls to CRM Web APIs stop working, check your certificates.