Saturday, October 12, 2013

Edit command bar / ribbon in Microsoft Dynamics CRM 2013

As Microsoft Dynamics CRM 2013 came out the ribbon was excluded with couple exceptions. Instead of ribbon we now have something that's called "command bar", well, because it is a command bar. However, you could think it as a "ribbon in disguise" because all modifications are still to be done through RibbonDiffXml. You can modify your RibbonDiffXml by hand or you can make your life much easier and use Ribbon Workbench software / solution. It will allow you to modify both your command bar and the ribbon. Some few entities / forms in Crm 2013 still use ribbons. Outlook client use ribbons in a certain depth as well so it's definitely something to keep in mind. I also recommend to take a look at the CRM 2013 SDK as it can help you to understand this whole concept.

Example

In this example I'm adding a simple button to my custom entity's command bar

RibbonDiffXml:


And here is what it looks like. Notice my custom webresource icon. You can find tons of icons in CRM SDK.




And this is how Ribbon Workbench looks like. I definitely recommend using it but of course it's always good to know what happens under the hood!



Resources:

Thursday, October 10, 2013

Business rules in Microsoft Dynamics CRM 2013

Business rules in Microsoft Dynamics CRM 2013


Microsoft Dynamics CRM 2013 has been released a little time ago with a lot of new features and changes.

Business rules are one of the new features in CRM 2013. Business rules are relatively easy, well, business rules, that you can create through the user interface. Let's say you want to show or hide a certain field depending on another field's value. That would have take some javascript coding with CRM 2011 but with CRM 2013 you can actually achieve this by using only UI. So no programming needed, which is of course always welcome - especially for programmers.

So what can you do with business rules? Below lies a short list of different possibilities

  • Show error message
  • Set field value
  • Set business required
  • Set visibility
  • Lock or unlock field
So very basic but indeed very helpful stuff. Let's take a look at how to actually implement a business rule.

Show or hide a field using business rules

Example:

I have created an xRM system which I use to keep track of my sport activities. I have created a custom entity type named "Sport activity". In the sport activity form I have a drop down list of different sport activity types (including running, cycling, badminton, football). I also have a "Distance" field in the form (hidden by default) and I want to show it only when I've selected a certain sport activity type (such as running or cycling).

So what I need to do is to create a new business rule that'll set distance field visible when the sport activity type equals running or cycling. I can start this by opening a form designer and double clicking the sport activity type -field. There I see Business Rules tab with the "New" button. Refer the picture below.


Click "New" and fill the form. My business rule looks like this



Notice the Scope option on a top right hand side corner. It means that this business rule applies for Information form by default. You can change the value to be "All forms" for example or if you've created a custom form you can set it as a scope.

When you're ready simply Activate your business rule and you're on the go.

Note that you have to create another business rule to hide the distance field in relevant cases. Otherwise it would never go away after the first time it is set visible.


Monday, October 7, 2013

How to display a loading gif until a javascript function has been completed

The day may come when you come across the situation where you have so heavy javascript function that it actually takes some time to finish. While the code is running it freezes the UI which is not very user friendly but what can we do? Well, I'd say we have three options.

  1. The first thing I'd do is to check the logic of the code. If something is slow (speaking of javascript) there's probably better way to do it. But in some cases that's not an option. You may have actually written pretty decent code or you just don't have enough time to fix the whole past two years of someone's poor coding. Then I'd move to option two.
  2. Option two is Web Workers.

    "A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background." 

    Sounds ideal. And it is. But remember that Web Workers are not supported by IE 8 or other slightly older browser that are still in heavy use. So what do we do now?
  3. My third option would be to add a loading gif / loading text to the screen so that a user at least can tell there's something happening even if he's not be able to use the page while the script is running. And this is the topic of this post. 

How to show loading gif on the screen until the function has completed

So I created very simplified solution for this not-a-real-world example. See my solution in action. My code is below. I tried to comment the code so that you could have a sense what's happening there. But in a nutshell what I did.

A recursive function that waits one millisecond every time it's been called. You could think this as a home-made FOR-loop since we're keeping track of the quantity of function calls. Anyways, you could have this without a counter so that It would run till the end of the world. It would not freeze the browser because the timeout.



I hope this will help you in a way or another.