{Quick Tip} Need to show entity image in your webresource? Think of using entityimage_url and not image content.

I was bit hesitant to write this blog to be honest as I thought it’s pretty mundane and must be quite known to everyone. And we had the same requirement for the customer. They wanted to show the entity image of a contact in a webresource.

The functionality as already implemented and I was just reviewing the code. However when I checked the code, I found there is quite a bit written to show the image.

  • The entityimage field of the account is queried to get the content
  • The whole content is then set using <image src=’data:image/png;base64,<content>’ />

Surprising it is but when I told them there is a better way to do it without querying the content and all, many seemed unware of it. Hence I thought this would be of help to someone faced with similar situation.

For entity image field, there is a separate field created called – “entityimage_url” which can be queried as well. Below is the WebAPI query to get the entity image of an account. Same can be used with any other entity.

https://powerappsforyou2020.crm.dynamics.com/api/data/v9.0/accounts(936B419F-9F4B-E111-BB8D-00155D03A715)?$select=entityimage_url

image

The final step is to set the image url in the src attribute of the image in webresource. And you can see it in your web resource.

image

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Using Patch function in canvas app to update a CDS record based on value of non-primary key field.

I have recently been conducting some trainings on canvas apps and one of the questions that came up during the session was regarding the Patch function in canvas app.

As the name suggests, you can use the Patch function to modify or create a record in datasource. If you are unaware of the Patch function in canvas app, I suggest to go through the Microsoft Docs link.

However the situation I am going to explain here is bit different. The situation was more of like this.

“There is a contact form spanning multiple screens. The user would enter all the details of the contact including the email address. Depending on the value of email address, an insert or an updated would be performed using the patch operation”.

If I analyze the requirement, basically I would require check if there is any existing contact with the same email address. If exists then I should update, otherwise I should create. Pretty simple huh? Yes it is simple. But specific example is missing in docs. Hence penning this blog in case it is helpful for someone.

For this example:

Datasource – Contacts table of CDS. However this logic can be applied to any data source.

I have kept a really simple single screen here. Mind you I haven’t used a Edit form or a create form. The real requirement was in-fact spanning multiple screens for collecting contact info but for this example I have kept it simple.

Below is the screenshot of the same. A very simple one indeed.

image

Once the button “Update to CRM” is clicked, the magic should happen. To meet the requirement, I use the below formula in the “On Select” property of the button.

image

Formula:

Patch(Contacts, {contactid : LookUp(Contacts, emailaddress1 = EmailText.Text).Contact}, {firstname: FirstName.Text, lastname: LastName.Text, address1_city: City.Text, emailaddress1: EmailText.Text, address1_stateorprovince: ContactState.Text});

If you observe the above formula carefully, I am using the LookUp function to search for a contact with a emailaddress and then getting the contact id. If there is no matching email, contactid would be blank and hence the record is created.

For matching contact it is updated.

Now the question arises – What if there are multiple records which match the criteria? In that case the LookUp function would return a reference to just one of them and the returned record would be updated.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

{Quick tip} Consecutive line breaks while showing alert in Dynamics 365

By this time all of us are familiar with Xrm.Navigation.openAlertDialog. What’s more is going to be there in that API. After all it’s an API which just shows some alert.

And yet the simplest of things sometime surprises us. The same experience I had today while dealing with a requirement. It was a simple alert statement where I need to show a  text with couple of consecutive line breaks.

Something like “This is the first line <Line break> <Line Break> This is the second line

Piece of cake right? All we need to do is assign variable with the below code and show it in the alert.

var alertText = “This is the first line. \n \n This is the second line.”;

I was pretty much optimistic that this would work. And this is what it came out.

image

As you can see there is a single break in between the two lines. While we may all have our arguments asking customer to be content with a single line break, it is pretty awkward to tell the customer that a simple thing such as this is not possible to make it work. I was kind of ashamed to say that it is not working. So devoted some more time to it.

A bit of research and then I understood there should be some characters in between two line breaks to make it work. Unfortunately “space” character of keyboard is not working. If you see my text statement, I already have a space in between line breaks.

And then suddenly a eureka moment. How about some invisible characters in between the two line breaks. So I went to the below website and copied some invisible space equivalent characters.

https://beautifuldingbats.com/whitespace/

And it worked. But it is highlight unlikely to continue like this. How about replicating this character in code. So below is what I did.

var alertText = “This is the first line. \n” + decodeURI("%E2%80%8A") +”\n This is the second line”;

And this is what we get.

image

Sounded pretty interesting and thought to share.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Working with large data sources in Canvas apps. Don’t forget about delegation in canvas apps

Canvas apps is certainly changing a customer’s view point on mobile rollout feasibility for their business. However more often than not, we as consultants come across customers having significant data in the system. And while we don’t think much about data volumes while dealing with model driven apps, the same cannot be said about canvas apps.

And why is that so? The reason is because of “Delegation”. A very detailed explanation of the delegation feature is explained in this docs link. However let’s see from the implementation perspective how does it affect the canvas app you build.

So the first question is – “What is delegation?”. Usually when we hear the term delegation, we think of something related to user access like delegate privileges and stuff. But delegation in canvas app is related to fetching of data from data sources.

Well canvas app have some really formulas built in to query and filter data sources, very much excel like. So what happens when you put a query similar to the one below in Canvas apps.

image

If you track the request in Fiddler, this is what you get

/api/data/v9.0/accounts?$filter=(name eq ‘Test’)&$select=accountid,name

As you can see canvas app delegates the query to data source and gets the results back. Kind of obvious right? After all if there are thousands of records in the system it would be extremely bad design to bring in all the records to client and then apply the filter.

But there are some situations where canvas apps need to do just that. And why is that so? As I have mentioned earlier since canvas apps supports excel like formula, not all filters can be applied directly to data source as that might not be supported. Let’s take an example where I want to show the average of the credit limit for all my accounts. Below is the formula for the same.

image

The moment I write this formula, I get a blue line below it and then get a delegation warning that it might not work correctly on large data sets. The reason is the formula cannot be delegate to data source.

Now the question arises – “Does Canvas app bring all the records on the client side and try to take the average?”. Well the answer is No. For performance benefit, canvas app will bring in only the first 500 records no matter how much data you have in the table. Even the 501th record would not be considered for average calculation in my query. Hence the average value may not reflect the correct value.

Trust me this have happened so many times with me where I received the complaint that data shown is not being correct.

Is this 500 limit set somewhere or is it hardcoded in the framework? Well it’s configurable till a maximum value of 2000.

To change this limit, go to File menu of your canvas app and then Settings –> Advanced settings –> Data row limit for non-delegable queries

image

So keep in mind this delegation stuff next time you query with large data sets.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Wondering how to create controls dynamically in Canvas App? Galleries can be a workaround

Before I even go ahead and add more on this. Spoiler alert!  This blog is not going to introduce you to a new API which allows you to add/ remove controls dynamically in a canvas app. As of the time of writing this blog, there is no API in canvas app to accomplish this feature similar to controls.Add() or controls.Remove() feature in ASP.NET.

Here I am going to discuss about a workaround that I recently implemented for a customer having similar requirement.

Long story short, the customer had a requirement like this –

  • On a canvas screen, there is a button to get the add checklists for a record. The number of checklists are dynamic and depends on user selection of other parameters.
  • Checklists can be of type textbox where the user can enter data manually or of type checkbox
  • one checklist of type text and the other of type checkbox are shown by default to the user
  • The user can add as many checklists they want and the instructions for each checklist is dynamic as well.

So let’s see how we can set-up a quick POC for this.

First I go ahead and create a new blank app.

Then I go ahead and insert a vertical gallery control. I rename the gallery to “ControlsGallery”.

image

image

I delete all the contents of the gallery and insert three controls.

  • Label  – To show dynamic instructions
  • Text Input – For text input checklist.
  • Checkbox – For checkbox checklist.

Also added button to add or remove checklist.

image

Our next task was to populate the Items of the controls gallery. But we had a requirement that by default couple of checklists should be shown to the user.

We use the App OnStart event to declare the collection.

image

ClearCollect(DynamicControls, {Type:"Textbox", Value:"", Instruction: "Default Checklist 1"}, {Type:"Checkbox", Value: "", Instruction: "Default Checklist 2"});

What we are doing here? We are basically creating a collection called “DynamicControls”. And in the collection we are inserting two items. Each item has three properties.

  • Type – To determine the type of checklist
  • Value – The value input of the checklist control
  • Instruction – The label to show for the checklist.

The next step is to set the Items collection of the Gallery to this DynamicControls collection.

image

Once the Items property is set, it’s now time to bind our control elements in the gallery to the corresponding properties of the collection. Below are the bindings of each.

1. Instruction Label binding

image

2. Textbox Binding

First we bind the “Default” property of the textbox to the value property of the Collection.

image

Then we bind the visible property to the below formula as shown in the screenshot.

image

3.  Checkbox binding

Bindings are similar to the Textbox input. Check for below screenshots.

image

image

We are all set with the bindings. I created some extra stuffs to actually add instructions at runtime using Add Checklist button. Below is the entire stuff in action.

I hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Configure Customer lookup to show either contacts or accounts in Canvas Apps

Happy new year to all my blog readers and this is my first blog of 2020. And how about starting with a simple one and that too on canvas apps!

So this was my customer requirement. They were designing a canvas app for their business users which was basically kind of contact management. And while designing the contact edit form, they needed to show a form similar to the below one.

image

important fields to consider in the above screenshot are “Parent entity” & “Is company contact”.

Parent Entity field is nothing but the Company name (parentcustomerid) field of the contact. “Is Company Contact” is a custom field of type boolean.

In short the requirement was if that if the contact is a company contact, then the Parent entity field should allow users only to select Accounts else it should allow users to select only contacts. We will basically accomplish the following.

  • Show account/ contact dynamically depending on business logic
  • While switching between accounts and contacts dynamically, you may face the issue where either for contact or account, the display text does not show up. We are going to resolve that too.

So we set the Items property of the combobox with the below formula.

If(IsCompanyContact.Value = true,[@Accounts].’Account Name’,[@Contacts].’Full Name’)

image

If you are not aware of [@Accounts] or [@Contacts] syntax, refer to the Microsoft Docs link to learn more about record references.

So far so good. So we run the application.

image

So when “Is Company Contact” is selected, Parent Entity shows up the accounts as expected (screenshot above). Trust me, that’s the easy part. The one that will throw you off is the next one.

When the checkbox is unchecked, it should be showing the contacts. But below is how it shows up. Basically the contacts are loading but they are showing blank without any attribute value although we have specified fullname of the contact to show up in our formula.

image

We missed a simple thing and trust me it can consume some precious time of your day. The trick is to set the “Display Fields” property of the combo box. By default it was set to name for us and that’s the reason why account was showing us.

image

We changed that to the below formula. And now it showed up fine.

If(IsCompanyContact.Value = true,["name"],["fullname"])

image

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

“File” data type in CDS. All you may need to know about it.

Well this one is pure serendipity. I was working for a customer and while creating a new field for an entity, I could see a new data type called – “File”.

image

Honestly I haven’t heard of any announcement regarding this data type before and I was quite intrigued about what it is this data type. So I decided to dig deeper.

As I was trying from the classic solutions area, I decided to move on to the solutions area in PowerApps maker portal and check if that data type is showing up. And it indeed shows up.

image

As Microsoft is recommending, you should be using PowerApps maker portal more often than not. And the reason is there are some stuffs which you can only perform in maker portal and not is classic solutions area.

In the powerapps portal, you can set the size of the field. By default, the upload size is 32 MB. However you can set it to a max of 128 MB.

Few more basic info before I dive deep.

Is this available for all OOB and custom entities?

Yes.

Can we have more than one field of type “File”?

Yes.

 

Very well. Now comes the disappointment. The field cannot be embedded on the form and it does not shown up either on classic or unified interface. However there is some good news as well.

As of now the field can be used in canvas apps and flows. So I went ahead and created a canvas app with my File data type. And it works like a charm

image

I wonder whether it will be ever exposed to the Model driven apps and if yes, I think it completely changes the dynamics of file storage in CDS. Suddenly you have an ace to play with outside Notes and SharePoint integration.

Well well, this is nice. But how about getting the data stored in these fields. As coding is my forte, I am always bit biased towards developers and here I am not let them high and dry.

Detailed information is provided in docs – https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/file-attributes

In the docs link, there is sample code to query using REST API. Although the class for querying using .NET SDK is mentioned, there is no sample code on how to download the file bytes using the same. Hence I thought of putting this sample code for the benefit of readers.

InitializeFileBlocksDownloadRequest initializeFile = new InitializeFileBlocksDownloadRequest(); 
            initializeFile.FileAttributeName = "cr269_filedatatype"; // attribute name 
            initializeFile.Target = new EntityReference("account", Guid.Parse("37F480BE-EA1F-EA11-A810-000D3A569DFF"));

            InitializeFileBlocksDownloadResponse initializeFileResponse = (InitializeFileBlocksDownloadResponse)service.Execute(initializeFile);

            Console.WriteLine($"File Name: {initializeFileResponse.FileName}"); 
             Console.WriteLine($"File size (bytes): {initializeFileResponse.FileSizeInBytes}");

            var fileContinuationToken = initializeFileResponse.FileContinuationToken;

            // code to downlod the file. 
            DownloadBlockRequest downloadRequest = new DownloadBlockRequest(); 
            downloadRequest.Offset = 0; 
            downloadRequest.BlockLength = (long)4 * 1024 * 1024; // can be max of 4 MB 
            downloadRequest.FileContinuationToken = fileContinuationToken;

            DownloadBlockResponse downloadBlockResponse = (DownloadBlockResponse)service.Execute(downloadRequest);

            byte[] fileBytes = downloadBlockResponse.Data;

Couple of important points with the above code. You can download a max of 16 MB in one call. For the remaining content you need to query again using the FileContinutationToken. The block size determines the maximum chunk that can be retrieved in further calls. The file bytes cannot be retrieved using RetrieveRequest or RetrieveMultipleRequest

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)