Saturday, February 25, 2012

Setting default lookup values with JavaScript in CRM

One of my favorite new features in CRM 2011 is the ability to include web resources. When writing JavaScript that interacts with forms, it allows the developer to write code once and easily reuse it on multiple forms.
A few examples of things I might include in a utility type file revolve around price lists and products. Many customers I’ve worked with only use this functionality at a high level. They usually maintain only one price list and don’t utilize anything beyond the basic product units. When this is the case, you can save users a few clicks when creating records by defaulting these values for them.
So a JavaScript file I plan on including in multiple forms would probably start out looking like this:
if (MyNamespace === undefined) {
    var MyNamespace = {};
}

//Return the value for a Unit Group lookup
MyNamespace.GetUOMSchedule = function () {
     var value = new Array();
     value[0] = new Object();
     //Set the GUID of the value to return
     value[0].id = "D3AE1E99-1051-49E0-8C00-EF1415D10ED4";
     //Set the name of the value to return
     value[0].name = "Default Unit";
     value[0].entityType = "uomschedule";
     return value;
}

//Return the value for a Default Unit lookup
MyNamespace.GetUOM = function () {
    var value = new Array();
    value[0] = new Object();
    //Set the GUID of the value to return
    value[0].id = "B2D9FA9A-D3F1-472B-89E6-BB2D52117BB3";
    //Set the name of the value to return
    value[0].name = "Primary Unit";
    value[0].entityType = "uom";
    return value;
}

//Return the a value for a Price List lookup
MyNamespace.GetDefaultPriceList = function () {
    var value = new Array();
    value[0] = new Object();
    //Set the GUID of the value to return
    value[0].id = "750F3F1D-BE56-E111-9BBC-000C29336979";
    value[0].name = "Default Price List";
    //Set the name of the value to return
    value[0].entityType = "pricelevel";
    return value;
}

Granted with this approach, you will have to look up the GUIDs and names of the default values you want to return for each Organization you plan on using this with. You could write something to dynamically retrieve the values without hard coding anything but on every form load it would end up resulting in additional trips to return the values.

When you're ready to use one of these methods on a particular form you only need to follow a few steps. First of which is remember to include this library in your form before the file that contains the actual event logic. Once you’ve done that you can simply call one of the methods by name without having to worry about the specifics for each.

On a Product form, if we wanted to default the Unit Group and Default Unit fields, we could call this method during our Form OnLoad event. We are only setting the default values if we are creating a new record (check the Form Type) as to not overwrite any values when it is not a new record.
MyNamespace.Product_OnLoad = function () {
     if (Xrm.Page.ui.getFormType() == 1) { //Create
          Xrm.Page.getAttribute('defaultuomscheduleid').setValue(MyNamespace.GetUOMSchedule());
          Xrm.Page.getAttribute('defaultuomid').setValue(MyNamespace.GetUOM());
     }
}

Now when we open a new Product record, the Unit Group and Default Unit fields will default to the the values we specified. It will save the end users having to click the lookup field and select a value every time they create a new product.