Tuesday, January 17, 2012

CRM 2011 Switching Forms

One of the custom entities we have in our CRM deployment is used to track System information.  As a result, we have to store LOTS of different data points about each customers installation (Usernames, ip addresses, etc).

Since we have multiple business units, all using the same Dynamics CRM deployment, we have different needs for each unit, but all stored in the same System Entity.  In CRM 4.0 we solved this by storing the data for individual systems on tabs, then show/hide tabs when the form loads.  This worked fine until we ran into the limitation of 8 tabs per form. Ugh.

In CRM 2011 we can now have multiple forms for the same entity, so I wanted to take advantage of this and have the Form Load even determine which form to load for the appropriate system type.

First, I created a new custom entity for System Type, then added a field on that entity for the name of the form to load.  Now when the System form loads, it queries the system type for the form name to use.

The heart of this routine is the call to:
Xrm.Page.ui.formSelector.items.get(sSystemMainFormId).navigate()
The trick is getting the GUIDs for all the forms.  For that, I built an array and then loop through it.

Hopefully you will find this useful.

function setCorrectSystemForm() {
    var sAlertMessage = '';

    var arrForms = new Array();
    var iFormCounter = 0;
    var sFormId = '';
    var oCurrentFormItem = Xrm.Page.ui.formSelector.getCurrentItem();
    var sCurrentForm = oCurrentFormItem.getLabel();
    var sCurrentFormId = oCurrentFormItem.getId();
    var sSystemMainFormId = '';


    // Load all the forms into an array
    Xrm.Page.ui.formSelector.items.forEach(
        function (item, index) {
            var itemLabel = item.getLabel();
            var itemId = item.getId();

            arrForms[iFormCounter] = new Object();
            arrForms[iFormCounter].name = item.getLabel();
            arrForms[iFormCounter].id = item.getId();
            sAlertMessage += "  \u2219 " + itemLabel + " :: " + itemId + "\n";
            iFormCounter++;

        });
    iFormCounter--;

    sAlertMessage += "\n\n iFormCounter = " + iFormCounter.toString();

    // Query for the form name required for this system
    var sFormType = getSystemTypeFormName();
    var sFormTypeId = '';


    // Match that name with an ID from our Array
    for (var i = 0; i <= iFormCounter; i++) {
        if (arrForms[i].name == sFormType) {
            sAlertMessage += '\n This System uses Form :: ' + arrForms[i].name + ' \n id :: ' + arrForms[i].id.toString();
            sFormTypeId = arrForms[i].id;
            break;
        }
    }


    // Get the default Main form ID in case we haven't assigned a System Type for this record.
    for (var i = 0; i <= iFormCounter; i++) {
        if (arrForms[i].name == 'System-Main') {
            sAlertMessage += '\n\n System-Main Form id :: ' + arrForms[i].id.toString();
            sSystemMainFormId = arrForms[i].id;
            break;
        }
    }


    sAlertMessage += '\n ';
    sAlertMessage += '\n sFormType :: ' + sFormType;
    sAlertMessage += '\n sFormTypeId :: ' + sFormTypeId;
    sAlertMessage += '\n ';
    sAlertMessage += '\n sCurrentForm :: ' + sCurrentForm;
    sAlertMessage += '\n sCurrentFormId :: ' + sCurrentFormId;


    // Compare the form type required to the current form and navigate as needed.
    if (sFormTypeId != '' && sFormTypeId != sCurrentFormId) {


        // Comment this out to get full debug information
        sAlertMessage = '';
        sAlertMessage += '\n Incorrect Form for this System \'Type\'.';
        sAlertMessage += '\n Loading Form ' + sFormType + ' ...';
        alert(sAlertMessage);

        // Navigate to the correct form
        Xrm.Page.ui.formSelector.items.get(sFormTypeId).navigate();
    } else {
        if (sFormTypeId == '' && sCurrentFormId != sSystemMainFormId) {
            sAlertMessage = '';
            sAlertMessage += '\n System \'Type\' Not Selected!\n'
            sAlertMessage += '\n Select a \'System Type\' to load correct form for this system...';
            sAlertMessage += '\n Defaulting to System-Main form.';
            alert(sAlertMessage);

            // Navigate to the default form
            Xrm.Page.ui.formSelector.items.get(sSystemMainFormId).navigate();
        }
    } // end else if no system type selected

};

No comments:

Post a Comment