locked
change forms using javascript RRS feed

Answers

  • I guess he means changing the current form used... There is an example in the sdk that use the navigate() method:

    showFormItems: function ()
    {  var items = Xrm.Page.ui.formSelector.items.get();
      if (items.length > 1)
      {
        var html = "<html><head><title>Show Form Items</title>";
        html += "<style type=\"text/css\">body { font-family:Calibri;}";
        html += "table {border:1px solid gray; border-collapse:collapse;}";
        html += "th {text-align:left; border:1px solid gray;}";
        html += "td {border:1px solid gray;}</style>";
        html += "<script type=\"text/jscript\" >";
        html += "function navigate(itemId) { ";
        html += "window.opener.Xrm.Page.ui.formSelector.items.get(itemId).navigate(); ";
        html += "window.close();";
        html += "}";
        html += "</script></head><body>";
        html += SDK.FormItemSamples.getFormItems();
        html += "</body></html>";
        var myWindow = window.open("", "_blank");
        myWindow.document.open();
        myWindow.document.write(html);
        myWindow.document.close();
      }
      else
      {
        alert("There is only one form item currently available.");
      }
    }
    

    My blog : http://mscrmtools.blogspot.com

    All my tools for Dynamics CRM 4.0 on my dedicated site: MSCRMTools Repository

    Friday, February 25, 2011 3:27 PM
    Moderator
  • Do you really need to use separate forms for this?

    If your goal is to minimize the amount of javascript required for hiding and showing fields depending of the product type you should look into creating a tab element for each product type instead. Just show/hide the tabs you want on the onload/onchange event - not alot of javascript is required for this.
    Take advantage of the possibility to add fields to the form several times and the new object model of the form.

    Emil

    • Proposed as answer by Philippe LEAL Wednesday, November 16, 2011 3:34 PM
    • Marked as answer by Michael Kolling Wednesday, November 16, 2011 3:40 PM
    Sunday, February 27, 2011 10:02 PM

All replies

  • Hello Michael,

    What exactly do you mean by change forms using JS?


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Friday, February 25, 2011 3:25 PM
  • I guess he means changing the current form used... There is an example in the sdk that use the navigate() method:

    showFormItems: function ()
    {  var items = Xrm.Page.ui.formSelector.items.get();
      if (items.length > 1)
      {
        var html = "<html><head><title>Show Form Items</title>";
        html += "<style type=\"text/css\">body { font-family:Calibri;}";
        html += "table {border:1px solid gray; border-collapse:collapse;}";
        html += "th {text-align:left; border:1px solid gray;}";
        html += "td {border:1px solid gray;}</style>";
        html += "<script type=\"text/jscript\" >";
        html += "function navigate(itemId) { ";
        html += "window.opener.Xrm.Page.ui.formSelector.items.get(itemId).navigate(); ";
        html += "window.close();";
        html += "}";
        html += "</script></head><body>";
        html += SDK.FormItemSamples.getFormItems();
        html += "</body></html>";
        var myWindow = window.open("", "_blank");
        myWindow.document.open();
        myWindow.document.write(html);
        myWindow.document.close();
      }
      else
      {
        alert("There is only one form item currently available.");
      }
    }
    

    My blog : http://mscrmtools.blogspot.com

    All my tools for Dynamics CRM 4.0 on my dedicated site: MSCRMTools Repository

    Friday, February 25, 2011 3:27 PM
    Moderator
  • Hi

    Yes I have two forms that i have built for Products.

    When I open a product, I want javascript to look at the producttypecode and switch to whichever form is the appropriate one for the kind of product.

    I almost got it there but infinite loop as it kept opening the form and then trying to switch.

    Code is currently here....

    function showFormItems()
    {
    var items = Xrm.Page.ui.formSelector.items.get();
    var screentype = Xrm.Page.ui.formSelector.getCurrentItem().getLabel()  ;
    var itemtype = Xrm.Page.getAttribute('producttypecode').getValue() ;
    var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();

    alert("FormScreentype:  " + screentype + "   FormScreenID:  " + currentFormId + "  ProductType: " + itemtype);

    if (itemtype == "1")
    {
        if (screentype == 'Clearances')
        {
            Xrm.Page.ui.formSelector.items.get(0).navigate();
        }

    }
             
    if (itemtype == "2")
    {
         if (screentype == 'Music')
          {
            Xrm.Page.ui.formSelector.items.get(1).navigate();
          }
    }

    }

    But I think the

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

    line is wrong?


    Mike Kolling
    Friday, February 25, 2011 3:28 PM
  • Do you really need to use separate forms for this?

    If your goal is to minimize the amount of javascript required for hiding and showing fields depending of the product type you should look into creating a tab element for each product type instead. Just show/hide the tabs you want on the onload/onchange event - not alot of javascript is required for this.
    Take advantage of the possibility to add fields to the form several times and the new object model of the form.

    Emil

    • Proposed as answer by Philippe LEAL Wednesday, November 16, 2011 3:34 PM
    • Marked as answer by Michael Kolling Wednesday, November 16, 2011 3:40 PM
    Sunday, February 27, 2011 10:02 PM
  • Hi All,

     

    Using the navigate function has its problem ,whenever we use the navigate function to particular formid,the forms get reloaded,

    and its a problem with CRM 2011, that it saves the last viewed form for a particular userid

    and it will show that form only.

    the table where it stores all this information is UserEntityUISettings table.

    So using multiple form has its problems

    thanks

    Abhishek

    Friday, December 9, 2011 12:50 PM
  • Hi

     

    We have similar requirement for our customer. We can live with the issue of last cached form. But we have an issue on dynamically switching form when an existing record is opened, we get "Error on page" and that only goes away by refreshing the form.

    error detail using dev tool is "SCRIPT5011: Can't execute code from a freed script "

    We have also tried using

    "window.location.reload(true);"

     

     

    but no luck.

     

    Any ideas please?

    Thanks

     

    Thursday, January 5, 2012 11:49 AM
  • Hi

     

    i finally managed to resolve the issue. Actually there was some other code being executed and the focus was moving to new form type whereas the previous one had not finished execution. Therefore i got around by introducing some delay.

    Hope it helps others as well

     

    Umer

    Friday, January 6, 2012 11:53 AM
  • Can you let us know what code you used to introduce the delay?
    Saturday, January 28, 2012 7:25 PM
  • Hi

     

      window.setTimeout('CreateActivity()', 1000);  

     

    • Proposed as answer by Umer_Javed Monday, January 30, 2012 3:30 PM
    Monday, January 30, 2012 3:29 PM
  • I'm seeing another problem with form switching (from the onLoad event) where it wont fire some of the onChange events after the form is switched.   And some of the ribbon button stop working.  I'm working with Cases also causes a lot of other issues (like the requirement to have a contract field on the form).

    SDK example that show form switching doesn't have this problem, because it open a separate windows and waits for the user to click.

    umer's setTimeout also works, but hate having to make my users wait an extra second for the page to load (re-load).



    Wednesday, February 1, 2012 3:54 PM
  • Hi,

    Just a suggestion,

    Instead  of adding “Timeout” script , we can do chk for pagelayout and exit function..

    Its worked for me.

    Here is the code

    function PageLoadScript()

    {

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

    var getNavigate = Xrm.Page.getAttribute('new_layoutname').getValue() ;

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

                    //to avoid infinite loop on pageload…

    if(getNavigate ==getCurrentScreen )

    {return;}

    for(i=0;i<=items.length-1;i++)

    {

    if(getNavigate == Xrm.Page.ui.formSelector.items.get(i).getLabel())

    {

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

    }

    }

    }

    Tuesday, June 5, 2012 6:04 AM
  • Hi,

    Just a suggestion,

    Instead  of adding “Timeout” script , we can do chk for pagelayout and exit function..

    Its worked for me.

    Here is the code

    function PageLoadScript()

    {

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

    var getNavigate = Xrm.Page.getAttribute('new_layoutname').getValue() ;

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

                    //to avoid infinite loop on pageload…

    if(getNavigate ==getCurrentScreen )

    {return;}

    for(i=0;i<=items.length-1;i++)

    {

    if(getNavigate == Xrm.Page.ui.formSelector.items.get(i).getLabel())

    {

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

    }

    }

    }

    Tuesday, June 5, 2012 6:04 AM
  • Hi, I have  similar situation where I created three forms for the account.  Based on the field, i want to show the correct form on load event. 

    Question: Did you enter the GUID ID in single quote or double quote or no quote?  

     Xrm.Page.ui.formSelector.items.get("2234-sdr230-werw205-wrw7").navigate();

    I am getting error. I tried with double quotes and no quote. 

    Tuesday, July 31, 2012 11:57 PM
  • Hi i am wondering where you get the page layout from? Is it a custom field you created? new_layoutname'

    Wednesday, August 1, 2012 12:38 AM
  • these Jscript isn't not reliable it seems. 
    Wednesday, August 1, 2012 12:39 AM
  • Thursday, January 31, 2013 8:33 PM
  • The SDK says that this attribute (BehaviorInBulkEditForm) is not supported.

    So JavaScripts won't run while bulk editing...

    Thursday, January 31, 2013 10:12 PM
  • One problem that no one ever seems to mention is that if, at design time, you uncheck "Show Navigation Items" (Form Properties/Display Tab/Page Navigation), then  Xrm.Page.ui.formSelector.items.get() returns an empty array and Xrm.Page.ui.formSelector.getCurrentItem() returns null.

    There is no way to tell what form you're on or to tell what other forms are available and most importantly Navigate DOES NOT WORK.

    To me that's a HUGE oversight that there appears to be no work around for.


    Louis

    Thursday, May 16, 2013 7:45 PM
  •  Below code is working in MS CRM 2013


    we can navigate from one main form to another main form through javascript as below code snippet.

    Using index of the form type:

             we can change the form type based on the index. In the above image Information index is 0 and Case index is 1.

     Code: 

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

           var labelText = formName.getLabel();

          Xrm.Page.ui.formSelector.items.get(1).navigate(); //Here I am navigating to Case form

    Using index of the form Form ID:

             we can change the form type based on the Form Id.  

     Code: 

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

           var labelText = formName.getLabel();

         var formID="f658c846-f74c-459b-b591-b97558958106";

          Xrm.Page.ui.formSelector.items.get(formID).navigate(); //Here I am navigating to Case form



    • Edited by Bangar Raju Wednesday, February 5, 2014 9:14 AM
    Wednesday, February 5, 2014 6:45 AM