Monday, June 11, 2012

How to display an entity form based on the option set field (Multiple forms CRM2011)

Sometimes we need multiple forms based on categorisation of an entity. For example, an account can be  a customer or a competitor or a supplier etc. It is possible that we need to capture or display different fields based on the type of an account. Suppose we have different forms  for different types of account. If the user opens an account record, the system should open a appropriate form based on the type of the account.
In this example I am using account entity. I have created 2 custom forms. Now I have 3 account forms (Information, Information1, Information2). In out of the box account entity . there is a “Relationship Type” or customertypecode field to categorise the account entity. It is picklist field. I want to open a different form based on the value of the customertypecode field . Here is the code.
function showForm() {

//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("customertypecode").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 = "Information1";
            break;
        case 2:
            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
The code is checking if the form is an  “Update” form and opens up the form based on the value of customertypecode field. The code is reloading the form only if the current form is different than the form the system should be displaying.
Call this function on form load event.
Happy programming…

19 comments:

  1. hi grate post i have done this and but small problem on loaded form. all the ribbon buttons are disabled any idea to resolve this

    ReplyDelete
    Replies
    1. further im using this to quote entity and also notice that once we active the record the form selector script not executing

      Delete
    2. I don't have access to CRM right now. I will check it for you.

      Delete
  2. I was having minor difficulties with the program and I was just looking for the solution. So, this is how it should have been done. It's been helpful, this code.

    ReplyDelete
  3. Microsoft Dynamics CRM training will help you manage and prioritize your business goals, customize.we teach MS Dynamics CRM training and class available at Hyderabad.

    ReplyDelete
  4. Arrowsol Training offers you a way to training materials, classroom training, E-Learning, certification exams, Microsoft Press Books, and more for Microsoft Dynamics.
    Microsoft Dynamics Training | Microsoft Dynamics online Training | Microsoft Dynamics Training online

    ReplyDelete
  5. Hello,

    This looks exactly like what I am need, however, when I run this on the Form Load event, I keep getting an error:

    The value of the property 'ShowForm' is null or undefined, not a Function object.

    I created a new javascript web resource called dmo_ShowForm and than add it as a Form Library. Then selected Control:Form and Event:Onload and Function:ShowForm

    I also tried replacing the "" with straight quotes and single quotes in case something in the copy/paste changed the quotes.

    In the error report it says Syntax error, line 39, however, that is just the end of the script and I have tried adding/remove braces } but doesn't seem to get rid of the error. Also tried alert(relType) to see if I was getting a value but the alert just gets ignored.

    Any ideas?

    ReplyDelete
    Replies
    1. Of course, after posting I was able to find what was causing the error.

      1. I had called the function in the script showForm but had called it ShowForm in the Function field of the CRM UI...case matters here.

      2. In line 1 of the script, if (Xrm.Page.ui.getFormType()==2) it is missing an open brace ({) at the end

      (Xrm.Page.ui.getFormType()==2) {

      Thanks again for posting this! It was very helpful!

      Mike

      Delete
    2. Thank you Amreek for the original script and MikeH for your revisions to the script. The script is working however, when I close out of the form I get an error. MikeH - would you be able to post your final script? I have attached mine in hopes someone could identify the error. Thanks!

      function showForm() {

      //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("casetypecode").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 = "Information1";
      break;
      case 2:
      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

      Delete
  6. Hello! Do you utilize any specific services to attract more readers your domain on a daily basis? Can't wait to hear from you.

    ReplyDelete
  7. Hi Amreek,
    We have used similar script to redirect between multiple forms which was working fine, after UR 13 is applied we are getting a save/discard alert. Any idea how to fix that?

    Regards
    Jeevan

    ReplyDelete
  8. Hi,

    Could you please help me out, how to show/hide the field based on radio button. How to get rid of Empty space of hidden field?

    ReplyDelete
    Replies
    1. Put that field in a section and hide the section using

      Xrm.Page.ui.tabs.get("TabName").section("SectionName").setVisible(false);

      Delete
  9. Can we switch between multiple Form without loading the form twice?

    ReplyDelete
  10. " Can we switch between multiple Form without loading the form twice?" - AnonymousJanuary 20, 2015 at 2:23 AM

    I'm also really curious to that! Has someone developed this kind of great functionality already or know its somehow possible?

    ReplyDelete