Javascript: Form Selector

by Ivan Gee 16. July 2012 06:20

CRM 2011 gives you the option to create multiple forms per entity, however, if you run into a situation where you need to display a specific form you will need to write some custom JavaScript to achieve this. 

· Example 1: You need to display a different form when a user creates a record or updates a record.

· Example 2: You need to display a different form based on an OptionSet value.

If you don’t want a user to view a specific form, you can assign security roles to forms to manage which users have access. By default, CRM 2011 loads the previously opened form when opening a record. This code can act as an extra layer of security and prevent the user from modifying the wrong form if a user has access to more than one.

Let’s say we already created an OptionSet that has three values of: (COLD, WARM, HOT) and three forms that have the same names as the option set values. Let’s get started with some code.

FORM_TYPE_CREATE,  FORM_TYPE_UPDATE: Determines what code to execute onLoad of the form.

COLD, WARM, HOT, INFO: Form GUIDs.

To get the GUIDs open up a record and press “F12” to open the debug window and type “document.frames[0].Xrm.Page.ui.formSelector.getCurrentItem().getId()” in the console window.

Xrm.Page.ui.formSelector.items.get(INFO).navigate() : Changes form to Information.

Use the Information form as the starting point when a new record is created. The formSelector function is an on load event and the onChangeType is an onChange function on the Type field.

Var currentFormName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel() : Get the label of the form to compare with the Type field.

Var type  = Xrm.Page.getAttribute(“new_type”) : The Option Set object of Type field. 

You now have code that will switch forms depending on OptionSet. Here’s the whole code:

var FORM_TYPE_CREATE = 1;

var FORM_TYPE_UPDATE = 2;

var COLD = "a0998c50-3bf4-4c45-8e87-be280a4a0b1d";

var WARM = "0ba8aa8e-9a04-4723-a1d2-1b1b6eee1ff7";

var HOT = "331a9681-834a-402a-bc0c-d042a179dfe1";

var INFO = "b053a39a-041a-4356-acef-ddf00182762b";

 

function formSelector() {

    if (Xrm.Page.ui.getFormType() == FORM_TYPE_CREATE) {

        Xrm.Page.ui.formSelector.items.get(INFO).navigate();

    }

 

    else if (Xrm.Page.ui.getFormType() == FORM_TYPE_UPDATE) {

        //current form name

        var currentFormName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();

        //Type field

        var type = Xrm.Page.getAttribute("new_type");

 

        //disable field so user wont change it (optional)

        Xrm.Page.ui.controls.get("new_type").setDisabled(true);

 

        //logic if Type field is null

        if (type.getValue() == null && currentFormName != "Information") {

            Xrm.Page.ui.formSelector.items.get(INFO).navigate();

        }

        else if (type.getValue() == null && currentFormName == "Information") {

            //enable field

            Xrm.Page.ui.controls.get("new_type").setDisabled(false);

        }

        //logic if type and form name dont match

        else if (type.getText() != currentFormName) {

            switch (type.getText()) {

 

                case "COLD":

                    Xrm.Page.ui.formSelector.items.get(COLD).navigate();

                    break;

                case "WARM":

                    Xrm.Page.ui.formSelector.items.get(WARM).navigate();

                    break;

                case "HOT":

                    Xrm.Page.ui.formSelector.items.get(HOT).navigate();

                    break;

            }

        }

    }

}

//save form

function onChangeType() {

    if (Xrm.Page.getAttribute("new_type").getValue())

        Xrm.Page.data.entity.save();

Tags: , ,

Categories: JavaScript

Comments are closed

Month List