July 25, 2014

Don't use getSelectedOption().text

OptionSet is a particular field in Dynamics CRM, basically is a key-value pair array, where the key is an integer and the value is a string.
Xrm Object provides several methods to interact with an OptionSet field, including the infamous getSelectedOption. But why a developer should avoid a supported Xrm method? The reason is simple, it can easily lead to JavaScript errors, as the getSelectedOption().text case.

getSelectedOption returns the selected option as an object with this structure:
// CustomerTypeCode OptionSet - Account Entity
{"value": 8, "text": "Prospect"}
It's true that we can access the object properties in order to get the label using the syntax getSelectedOption().text (and getSelectedOption().value) but only if the OptionSet field is not empty. If there isn't a value selected, the getSelectedOption() will return null and not an object.
// this code generates an error if the field is empty
var customerType = Xrm.Page.getAttribute("customertypecode").getSelectedOption().text;
A workaround is to check if the object is not null:
var customerType = "";
var option = Xrm.Page.getAttribute("customertypecode").getSelectedOption();
if (option != null) {
    customerType = option.text;
}
But Xrm object provides a separate method to retrieve the text of a selected OptionSet, this method is getText, and more important when no option is selected, getText will return an empty string value.

Error-free code:
var customerType = Xrm.Page.getAttribute("customertypecode").getText();
In the same way the standard getValue() must be used instead of getSelectedOption().value, it returns the integer selected value or null if the field is empty:
var customerValue = Xrm.Page.getAttribute("customertypecode").getValue();
if (customerValue != null) {
    // ...
} else {
    // ...
}
Note that getSelectedOption() can be still used in some scenarios, for example when we manipulate the OptionSet using AddOption,RemoveOption or getOptions methods and we want to compare the selected option as object with other values, but never use it to just retrieve the selected OptionSet label.

July 17, 2014

CRM 2013: Update Rollup or Service Pack?

Microsoft just released Update Rollup 3 for Dynamics CRM 2013 (download here also listed in my resources page) and I want to clarify the current situation regarding update rollups and service packs.

With the release of the Service Pack 1 Microsoft incremented the product number version, from 6.00 to 6.01, but with the Update Rollup 3 (related to the 6.00 version) they make official the division in two routines, one with the SP1 installed, one without the SP1.

The current paths are:
1) CRM 2013 RTM (6.00) → UR1 (6.00) → UR2 (6.00) → UR3 (6.00) → ... (6.00)
2) CRM 2013 RTM (6.00) → UR1 (6.00) → UR2 (6.00) → SP1 (6.01) → ... (6.01)

The confirmation is inside the KB for the Update Rollup 3:

Important notes
If Update Rollup 3 is applied, we recommend that you wait until Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 (SP1) is available before you upgrade to the Service Pack 1 release stream. This is because Update Rollup 3 may contain fixes that are not included in Microsoft Dynamics CRM 2013 SP1.

Bottom line: if you already installed SP1 in your environment, you need to wait for the release of the specific Update Rollup for SP1.