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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Select which form to display based on OptionSet Value

(0) ShareShare
ReportReport
Posted on by 2

Hi all, 

I am struggling with this (hopefully very simple) problem. 

I have created a global option set on the contact entity called Type Of Contact.

The options are

student

teacher

staff

I have 2 forms called

Student Form

Staff/Teacher Form.

I would like to use the script below to choose the student form when Type of contact = student and Staff/Teacher form when Type of contact = Staff or teacher.

I have created an onload web resource but I cannot get the code quite right. I understand this will give me the double load when the record is loaded.

The default form is the Student Form.

I am very new to jscripting so any help would be great.

function displaycorrectForm() {

//if the form is update form

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("new_typeofcontact").getValue();

// switch statement to assign the form to the picklist value

//change the switch statement based on the forms numbers and picklist values

switch (relType) {

case 1:

lblForm = "is this the option set value or the form name";

break;

case 2:

lblForm = "is this the option set value or the form name";

break;

default:

lblForm = "Default Lead";

}

// Current form's label

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

//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) {

//Check the current form is the same form to be redirected.

if(itemLabel != formLabel) {

//navigate to the form

item.navigate();

} //endif

} //endif

} //end for

} //endif

} //endif

} //end function

*This post is locked for comments

I have the same question (0)
  • Rahi9 Profile Picture
    15 on at
    RE: Select which form to display based on OptionSet Value

    Hi Francesco,

    Can you please show how you add this to the form with a few screenshot?

  • Ben Humphries Profile Picture
    2 on at
    RE: Select which form to display based on OptionSet Value

    You to.

    Thanks again.

  • Community Member Profile Picture
    on at
    RE: Select which form to display based on OptionSet Value

    Hi Ben,

    im glad you solved!

    Have a nice day and a good year.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Verified answer
    Ben Humphries Profile Picture
    2 on at
    RE: Select which form to display based on OptionSet Value

    Hi Francesco

    Got it working!! Thank you very much for your help!

    I have highlighted in Green the parts that i changed...

    The code is as follows

    var FormSwitcher = FormSwitcher || {};

    FormSwitcher._defaultFormName = "Student Contact Form";

    FormSwitcher._redirectToForm = function (formName, tryDefault) {

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

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

       // if we dont check to see if we're already on the form we'll get into an infinite loop

       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(); // redirect once we find it

                   return true;

               }

           }

       }

       else {

           return false;

       }

       // nothing found, redirect to default

       if (tryDefault && currentForm.getLabel().toLowerCase() != FormSwitcher._defaultFormName) {

           return FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);

       }

       return false;

    }

    function FormOnLoad()

    {

       var typeofcontact_STAFF = option set value without the commas;

       var typeofcontact_TEACHER = 760450003;

       var typeofcontact = Xrm.Page.getAttribute('publisher Prefix_typeofcontact').getValue();

       switch (typeofcontact)

       {

           case typeofcontact_STAFF:

               FormSwitcher._redirectToForm("Staff Contact Form", true);

               break;

           case typeofcontact_TEACHER:

               FormSwitcher._redirectToForm("Teacher Contact Form", true);

               break;

           default:

               FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);

               break;

       }

    }

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Select which form to display based on OptionSet Value

    Hi Ben ,

    i saw your video: as you said it seems all right.

    Have you tried to debug your script? It seems the only thing to do considering that there is no errer.

    The most probably issue i can imagine is related to right form name (eg spaces or something like that).

    Check also the optionset value are right (1 - 2).

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Ben Humphries Profile Picture
    2 on at
    RE: Select which form to display based on OptionSet Value

    Hi Francesco, I have sent you the message through linkedin.

    Thanks again

    Ben

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Select which form to display based on OptionSet Value

    Hi Ben,

    please share your video at info at francescopicchi dot com.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Ben Humphries Profile Picture
    2 on at
    RE: Select which form to display based on OptionSet Value

    Hi Francesco,

    Thank you very much for your help, I have added your code to my form and although it isnt giving me an error it also isnt changing the form based on the typeofcontact option set.

    I have taken a video of the process. Please can i share this with you privately.

    Thanks in advance

    Ben

  • Community Member Profile Picture
    on at
    RE: Select which form to display based on OptionSet Value

    Hi Ben ,

    this is the full working code.

    Please let me know if you solve:

    var FormSwitcher = FormSwitcher || {};
    FormSwitcher._defaultFormName = "Information";
    
    FormSwitcher._redirectToForm = function (formName, tryDefault) {
        var currentForm = Xrm.Page.ui.formSelector.getCurrentItem();
        var availableForms = Xrm.Page.ui.formSelector.items.get();
    
        // if we dont check to see if we're already on the form we'll get into an infinite loop 
        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(); // redirect once we find it 
                    return true;
                }
            }
        }
        else {
            return false;
        }
    
        // nothing found, redirect to default 
        if (tryDefault && currentForm.getLabel().toLowerCase() != FormSwitcher._defaultFormName) {
            return FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
        }
    
        return false;
    }
    
    function FormOnLoad()
    {
        var typeofcontact_STAFF = 1;
        var typeofcontact_TEACHER = 2;
        var typeofcontact_STUDENT = 3;
        var typeofcontact = Xrm.Page.getAttribute('typeofcontact').getValue();
    
        switch (typeofcontact)
        {
            case typeofcontact_STAFF:
                FormSwitcher._redirectToForm("Staff Form", true);
                break;
            case typeofcontact_TEACHER:
                FormSwitcher._redirectToForm("Teacher Form", true);
                break;
            case typeofcontact_STUDENT:
                FormSwitcher._redirectToForm("Student Form", true);
                break;
            default:
                FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
                break;
        }
    }

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Ben Humphries Profile Picture
    2 on at
    RE: Select which form to display based on OptionSet Value

    Thank you Francesco,

    So do i need something like....

    var FormSwitcher = FormSwitcher || {};

    FormSwitcher._defaultFormName = "Student Form";

    FormSwitcher.redirectAccountForm = function () {

       var formSelector = Xrm.Page.ui.formSelector;

       if (formSelector) {

           var switchTo = FormSwitcher._defaultFormName;

    Whats goes here?

    if typeofcontact = staff 

    {

    choose "staff/teacher" contact form;

    }

    else if

    {

    if typeofcontact = Teacher

    {

    choose "staff/teacher contact form;

    }

    else

    if typeofcontact = Student

    {

    choose "Student contact form;

    }

           // do logic here to check which form to use and set the switchTo variable

           // once you've figured out the form to switch to, call the internal redirect method to switch the form

           FormSwitcher._redirectToForm(switchTo, true);

       }

    }

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans