web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Changing a Form based on option set Value

(0) ShareShare
ReportReport
Posted on by 274

Hello,

I have 4 forms in an entity. I created a default form, based on the option set value I would like to change the form.  I tried to use the below code on the default form, but nothing is happening. Do I need to have 4 if blocks with the associated option set number? Thanks!

function showForm() {

       
        if (Xrm.Page.ui.getFormType()==2){
            // variable to store the name of the form
            var lblForm;

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

      
        switch (relType) {
            case 1:
                lblForm = "Form1";
                break;
            case 2:
                lblForm = "Form2";
                break;
            case 3:
                lblForm = "Form3";
                break;
            case 4:
                lblForm = "Form4";
                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

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Neil Parkhurst Profile Picture
    10,727 User Group Leader on at

    One thing to watch is what will happen with your code if the formtype isn't an update form. (2). As then relType wouldn't be set.

    Try using this function to change form ...

    ChangeForm("Test Form");

    function ChangeForm(formName) {

     var currentForm = Xrm.Page.ui.formSelector.getCurrentItem();

     var availableForms = Xrm.Page.ui.formSelector.items.get();

     if (currentForm.getLabel().toLowerCase() != formName.toLowerCase()) {

       for (var i in availableForms) {

         var form = availableForms[i];

         // try to find a form based on the name

         if (form.getLabel().toLowerCase() == formName.toLowerCase()) {

           form.navigate();

           return true;

         }

       }

     }

    }

  • MituCRMing Profile Picture
    274 on at

    Thank you Nelil. Can I run this on change of field?

  • Suggested answer
    Neil Parkhurst Profile Picture
    10,727 User Group Leader on at

    In my application I have used "this" code on form load. I look at an optionset and ensure the correct form has been loaded. (

    What you need to do is very similar. Just called from the OnChange event on the optionset. So you should be able to adapt the idea to work in conjunction with your optionset OnChange. (Note: I am unsure of what will happen to any unsaved changes if you force a change of form like this when the form is dirty. You might want to consider that!)

  • Suggested answer
    Community Member Profile Picture
    on at

    Make sure that this code is present on all four forms.

    function showForm() {
            // variable to store the name of the form
            var lblForm;
    
            // get the value picklist field
            var relType = Xrm.Page.getAttribute("optionset").getValue();
               
            switch (relType) {
                case 1:
                    lblForm = "Form1";
                    break;
                case 2:
                    lblForm = "Form2";
                    break;
                case 3:
                    lblForm = "Form3";
                    break;
                case 4:
                    lblForm = "Form4";
                    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
  • Suggested answer
    Community Member Profile Picture
    on at

    @Neil Parkhurst: Perfect

  • Suggested answer
    shatha rabaya Profile Picture
    5 on at

    you should change the values of option set (from prosperities) to 1 , 2 , 3 and 4

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans