Friday, December 23, 2016

Set the Default form in MS CRM

If we have more than one Form in an Entity and want to set a default form on Create/Update. Currently The below code will show if we are creating a New record then it will automatically redirect to Account-Create form

//Navigate to Account-Create Form onLoad
function NavigateToAccountCreateForm() {
    if (Xrm.Page.ui.getFormType() == 1) {
        var formLabelName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
        if (formLabelName != "Account-Create") {
            var infIndex = null;
            Xrm.Page.ui.formSelector.items.forEach(function (item, index) {
                var itemLabelName = item.getLabel();
                if (itemLabelName == "Account-Create")
                { infIndex = index; }
            });

            if (infIndex != null) { Xrm.Page.ui.formSelector.items.get(infIndex).navigate(); }
            else { alert("Account-Create form not found. You cannot create new Account, Please contact your system administrator."); }
        }
    }

}

Another Method

function showDefaultForm() {
    //if the form is Create form
    if (Xrm.Page.ui.getFormType() == 1) {
        //check if the current form is Information Form
        if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != "Information") {
            var items = Xrm.Page.ui.formSelector.items.get();
            for (var i in items) {
                var item = items[i];
                var itemId = item.getId();
                var itemLabel = item.getLabel()
                if (itemLabel == "Information") {
                    //navigate to the form
                    item.navigate();
                } //endif
            } //end for
        } //endif
    }
    //if the form is update form
    if (Xrm.Page.ui.getFormType() == 2 || Xrm.Page.ui.getFormType() == 4) {
        // variable to store the name of the form
        var lblForm;

        // get the value picklist field
        var relType = Xrm.Page.getAttribute("hsl_formtype").getValue();

        // switch statement to assign the form to the picklist value
        //change the switch statement based on the forms numbers and Formtype values
        switch (relType) {
            case 864630000:
                lblForm = "Information0";
                break;
            case 864630001:
                lblForm = "Information1";
                break;
            case null:
                lblForm = "Information2";
                break;
            default:
                lblForm = "Information";
        }
        //check if the current form is form need to be displayed based on the value
        if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != lblForm) {
            var items = Xrm.Page.ui.formSelector.items.get();
            for (var i in items) {
                var item = items[i];
                var itemId = item.getId();
                var itemLabel = item.getLabel()

                if (itemLabel == lblForm) {
                    //navigate to the form
                    item.navigate();
                } //endif
            } //end for
        } //endif
    }//endif

} //end function

No comments:

Post a Comment