June 28, 2013

OptionSet JavaScript Helper Library

Xrm.Page object provides several methods to work with OptionSet attributes, the most used are:
  • Xrm.Page.getAttribute("optionset").getValue() - returns the selected item value
  • Xrm.Page.getAttribute("optionset").getText() - returns the selected item label
Sometimes is necessary to access all the items of an Optionset and not only the selected one, for this purpose I wrote a JavaScript library.

The library uses only supported JavaScript and doesn't query the metadata to retrieve the labels. It contains the following functions:

  • opt.GetValues(fieldName)
    returns an array with all the values of a specific OptionSet
    var freightTermsValues = opt.getValues("address1_freighttermscode");
    alert("Values inside Freight Terms OptionSet: " + freightTermsValues);
    
  • opt.GetLabels(fieldName)
    returns an array with all the labels of a specific OptionSet
    var shippingMethods = opt.getLabels("address1_shippingmethodcode");
    alert("Available Shipping Methods: " + shippingMethods);
    
  • opt.GetLabel(fieldName, value)
    returns the corresponding label for a specific OptionSet value
    var paymentTermLabel = opt.getLabel("paymenttermscode", 2);
    alert("Payment Term with Value 2 is: " + paymentTermLabel);
    
  • opt.GetValue(fieldName, label)
    returns the corresponding value for a specific OptionSet label
    var primaryValue = opt.getValue("address1_addresstypecode", "Primary");
    alert("Value for address type Primary is: " + primaryValue);
    
Library code:
if (typeof (opt) == "undefined") { opt = {}; }

opt.getValues = function (fieldName) {
    var values = [];
    var attribute = Xrm.Page.getAttribute(fieldName);
    if (attribute != null && attribute.getAttributeType() == "optionset") {
        var options = attribute.getOptions();
        for (var i in options) {
            if (options[i].value != "null") values.push(options[i].value * 1);
        }
    }
    return values;
};

opt.getLabels = function (fieldName) {
    var labels = [];
    var attribute = Xrm.Page.getAttribute(fieldName);
    if (attribute != null && attribute.getAttributeType() == "optionset") {
        var options = attribute.getOptions();
        for (var i in options) {
            if (options[i].value != "null") labels.push(options[i].text);
        }
    }
    return labels;
};

opt.getLabel = function (fieldName, value) {
    var label = "";
    var attribute = Xrm.Page.getAttribute(fieldName);
    if (attribute != null && attribute.getAttributeType() == "optionset") {
        var option = attribute.getOption(value);
        if (option != null) label = option.text;
    }
    return label;
};

opt.getValue = function (fieldName, label) {
    if (label == "") return null;
    var value = null;
    var attribute = Xrm.Page.getAttribute(fieldName);
    if (attribute != null && attribute.getAttributeType() == "optionset") {
        var options = attribute.getOptions();
        for (var i in options) {
            if (options[i].text == label) return options[i].value * 1;
        }
    }
    return value;
};
Note: developed with Xrm JavaScript Dojo

June 26, 2013

Book review: Microsoft Dynamics CRM 2011 Cookbook

These days I got the chance to read Microsoft Dynamics CRM 2011 Cookbook, a book written by Dipankar Bhattacharya.
Microsoft Dynamics CRM 2011 Cookbook
The book has 10 chapters and is structured as a list of recipes, covering different arguments about Dynamics CRM.

Chapter 1: Installing Dynamics CRM 2011
This is the largest chapter, it not only covers the installation of Dynamics CRM Server but also other components such as Reporting Extensions, E-mail Router, Outlook Client. Step by step instructions and appropriate screenshots.

Chapter 2: Maintaining and Optimizing Microsoft Dynamics CRM 2011 Server
A basic knowledge of SQL Server is required for this chapter, especially for the Backup & Recovering part, useful the sections explaining how to enable tracing.

Chapter 3: Administering Microsoft Dynamics CRM 2011
The first three recipes assist the users to configure SSL, Claims-based authentication (AD FS) and Internet-facing Deployment (IFD), the other recipes describe how to manage business unit, users and security roles.

Chapter 4: Data Management
This chapter goes through importing and exporting data, a nice addition the section about Auditing.

Chapter 5: Solution Management
Explain (and implementing) Dynamics CRM Solutions isn't an easy job, probably a book can be written regarding this argument. The author gives a good overview, introducing also the translation process.

Chapter 6: Entity Customizations and Chapter 7: Form and View Customizations
These 2 chapters explain how to customize CRM, the concepts inside are deeply connected (you can customize CRM without installing it, but you can't customize forms and views without a good knowledge about fields and relationships). The content can look obvious to a Dynamics CRM developer, but it contains tips about Rollup Update 12 and the new Process Driven Forms.

Chapter 8: Site Map and Ribbon Customizations
The chapter describes how to edit the Site Map and Ribbon Definition XML, actually there are several tools that can be used to perform these modifications, but these pages can be considered a good reference.

Chapter 9: Office and SharePoint Integration
Well, I don't face SharePoint integrations every day, so probably next time I will use this recipe to guide me. The chapter covers also the Mail Merge functionality, both arguments are very interesting.

Chapter 10: Processes
The final chapter is the real surprise of this book. Workflows and Dialogs are very powerful and inside these pages are well explained. This part is a must-read also for end users who create or maintain processes in their CRM.
Note: the chapter doesn't cover the creation of Custom Workflow Activities.

Final score

Final Thoughts
A technical book is not a romance, this means skipping chapters or read over and over again the same pages. The variety of the arguments makes the book interestng for those who want to expand their knowledge about Dynamics CRM (a developer curious about the installation process) but also for those who face Dynamics CRM in the early stages of a project.

Where to buy