Prefill Lookup Field with Default Value Dynamics CRM 2011

Paul Nieuwelaar, 05 February 2012

As promised in a previous post on prefilling fields in Dynamics CRM 2011, I was going to show how to prefill a lookup field in Dynamics CRM 2011. In this post I will finally be showing how to do this, so that when you create a new record for a specific entity, the lookup field is prefilled with a reference to an existing record that you’ve defined.

 Prefill Lookup Field with Default Value Dynamics CRM 2011

One example of where this might be useful is on an Account, defaulting the Price List lookup to ensure new Accounts are set with the selected Price List entered. In this case we only have 1 standard price list, so we can simply set every account’s Price List field to the Standard Price List record.

You will need to create a new Web Resource to hold the JScript required for doing this. This JScript Library should contain the following code:

// Prefill a Lookup field with a default value
function prefillLookup(lookup, text, firstTabIndex) {
    var lookupAttribute = Xrm.Page.getAttribute(lookup);
    var lookupControl = Xrm.Page.ui.controls.get(lookup);
    var lookupElement = document.getElementById(lookup + "_ledit");
    var isCreateForm = Xrm.Page.ui.getFormType() == 1;
    // Only prefill if its a new record, and the lookup is empty
    if (isCreateForm && lookupAttribute.getValue() == null) {
        // Set focus to the Lookup Control
        lookupControl.setFocus();
        // Prefill the text into the Element (unsupported)
        lookupElement.value = text;
        // Fire onblur to resolve the text (unsupported)
        lookupElement.fireEvent("onblur");
        // Wait 500ms for the lookup to resolve, then click first tab
        setTimeout(function () { clickFirstTab(firstTabIndex) }, 500);
    }
}
// Clicks first tab, so that if the lookup is on another tab, the first tab will still be selected
function clickFirstTab(firstTabIndex) {
    // Unless specified, use tab 0 (the default first tab)
    if (firstTabIndex == null) { firstTabIndex = 0; }
    // Removes focus from the Lookup field
    document.activeElement.blur();
    // Click the first tab
    document.getElementById("tab" + firstTabIndex + "Tab").fireEvent("onclick");
}

Once the JScript Library is created, add it to your form, in this case Account. Add an event handler to the Form OnLoad, and enter the parameters. The parameters should be in the format: lookup, text, firstTabIndex. ‘lookup’ is the schema name of the Lookup field, ‘text’ is the name value that will be prefilled, and ‘firstTabIndex’ is used if you have other JScript hiding the first tab, in which case you would enter the index value of the tab at the top of the form. If there are no other scripts hiding the first tab, you can simply ignore that parameter and it will automatically use the first.

  Prefill Lookup Field with Default Value Dynamics CRM 2011

Save and Publish your form, and then check the results. Do not test using the preview form as it does not work correctly there, instead create a new record. If it’s working correctly the lookup value should be prefilled, and the focus should be back on the first tab. If there are multiple records with the same name value as the default text, it will not be resolved automatically, and you would need to manually resolve it.

 Prefill Lookup Field with Default Value Dynamics CRM 2011

Please note some of the JScript methods used here are unsupported. Also note that once the lookup has been prefilled, focus will not be returned to the first field on the form, and so you would need to click into the field again before typing. This function can also be called multiple times on the same form for prefilling multiple lookups at once if required.