The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

Microsoft Dynamics CRM and sample code: Think First

Microsoft Dynamics CRM and sample code: Think First

  • Comments 10

CRM MVP Michael Höhne is our guest blogger today. You can read more by him on his blog.

Here's one of my favorite samples floating around in the CRM world:

         image

I have seen this code numerous times and before explaining what's wrong (actually everything is wrong, besides the alerts), I want to tell you a story, which is also told by the above code.

You enter a mall and buy a basket, because you want to see if there is an apple in it. However, after leaving the mall you throw the basket away without using it. Back at home you search for an existing basket, take the first apple out of it and try to figure out if it is red. You then wonder why you cannot determine the color, because you forgot to check if there was a basket at all.

That's what the code does!

  1. There is no reason to initialize the lookup variable with a new Array when setting it to null in the next line. This is the basket you are buying and instantly throwing away.
  2. Even setting it to null is nonsense when setting it to crmForm.all.customerid.DataValue afterwards.
  3. The DataValue property is null if no value is stored in a lookup field. Therefore accessing lookup[0] results in a null reference exception if DataValue equals null, because the lookup variable then will be null as well. It is the same as grabbing the first apple from a basket without having the basket first.
  4. If there is a value stored in the lookup field, then lookup[0] won't be null. The only way it could be null is you setting it to null in code (crmForm.all.customerid.DataValue[0] = null), but that would be like writing var circle = null and wondering why circle.radius doesn't work.

To summarize: samples are great. But be sure to understand what they do first.

Cheers,

Michael Höhne



  • You've raised a valid point Michael, I think it all boils down to inexperience, as a solution there should be a Wiki with best practices and little how to guides which are moderated by people like Michael and other experienced developers to avoid this kind of code.

  • brilliant summary michael.  i too am frustrated by how often I see this specific example and have to fix it... even amongst experienced developers!

    perhaps a follow-up article on building your own library of code snippets to refer to instead of grabbing the first google result should be planned as part 2?

  • Nice one :)

  • You are right, but it would be helpful to provide a sample that is correct.

    So the blind copy-pasters amongst us can learn a lesson.

  • Hey Michael:

    Great article and insights, however I think a good ending would have been to show the correct code! :)

    John.

  • Agreed.  Let's see the correct code.:)

  • There is not a single correct code, but I agree that I should have added some examples:

    if (crmForm.all.customerid.DataValue != null) {

       var customer = crmForm.all.customerid.DataValue[0];

       alert(customer.name);

       alert(customer.id);

       alert(customer.typename);

    }

    One could argue that this code will break when the customerid field is removed from the form. If you want to be safe, then use

    if (crmForm.all.customerid != null && crmForm.all.customerid.DataValue != null) {

       var customer = crmForm.all.customerid.DataValue[0];

       alert(customer.name);

       alert(customer.id);

       alert(customer.typename);

    }

    if (crmForm.all.customerid != null ... checks for the existence of the customerid field, while ... && crmForm.all.customerid.DataValue != null) checks if the DataValue property of the customerid field is not null.

    Like many other programming languages, JavaScript (or JScript) doesn't perform a complete boolean evaluation. If crmForm.all.customerid is null, then the result of the entire boolean expression is known and the remainder (crmForm.all.customerid.DataValue != null) is not evaluated anymore. This is important, because otherwise it would lead to an error (crmForm.all.customerid is null and null.DataValue obviously doesn't work).

    An example of a programming language that does a complete Boolean evaluation is Basic, including VB.NET.

  • great article, but I would add that having three alerts one after another would drive me crazy. I would do it using one alert, separating them by string descriptions of each value you display. Something like this:

    alert(´name: ´ + customer.name + ´; id: ´ + customer.id + ´; typename: ´ + customer.typename);

    Anyway, it is not wrong to have three alerts but semantics.

  • excellent point, and the correct code reads so much better.  I also dislike the use of pointless constants,  I see the following used in a lot of code snippets,  for example

    var CRM_FORM_TYPE_CREATE = 1;

    if (crmForm.FormType == CRM_FORM_TYPE_CREATE){

    ...

    }

    While the above is not wrong IMHO I feel the following reads better, especially if the form type is checked multiple times on the form.  

    var isCrmFormCreate = crmForm.FormType == 1;

    if (isCrmFormCreate){

    ...

    }

  • Am dammm new to CRM topic, plz suggest me a book which I can start my work (coding),am confused where I have to code in CRM.

    If I import any form from the CRM ,where should i save and code and republish my form again in CRM... I have lot of question in my mind,can any one give a solution. Thanks in advance.

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post