Thursday 15 August 2013

How to Open a new Form & Set Field Values?

Here's a short post demonstrating how you can use JScript to open a new CRM form window, and set the field values in CRM 2011.

There are two methods to achieve this. Both involve using JavaScript, however selecting the best approach depends on what you want to do with the window once opened?

Method A - Xrm.Utility.openEntityForm 
This uses the Xrm utility helper object provided as part of the CRM platform. Under the hood, it is using the same call as method 2 to launch the new window. But the benefits are that it eliminates the need of creating and encoding the URL to the entity type you want to open.

Disadvantages are that you get no handle to the newly opened window.

Method B - window.open 
This is the standard JavaScript method of launching a new window. All you old school developers should be familiar with this one :) 
The disadvantage with this approach is that you have to construct the correct URL in order to launch the correct CRM Form. 

The benefit with this method is that you can obtain a handle to the new window and perform the actions you JavaScript junkies are accustomed too.

Example Solutions
The following are examples of both methods using the Contact entity for demonstration purposes:


  1. function openNewForm_MethodA()
  2. {
  3.     var params = {};
  4.     
  5.     //Set Parent Customer Lookup Field
  6.     params["parentcustomerid"] = "EA27A654-79EA-400B-9DE6-84C8E11394B4";
  7.     params["parentcustomeridname"] = "My Account Name";
  8.     params["parentcustomeridtype"] = "account";
  9.     
  10.     //Set Standard Text Fields
  11.     params["firstname"] = "John";
  12.     params["lastname"] = "Smith";
  13.     params["mobilephone"] = "1234567890";
  14.     params["emailaddress1"] = "mail@domain.com";
  15.         
  16.     //Open new Window/CRM Form
  17.     Xrm.Utility.openEntityForm("contact", null, params);
  18. }
  19.  
  20. function openNewForm_MethodB()
  21. {
  22.     //Set Parent Customer Lookup Field
  23.     var extraqs = "parentcustomerid={EA27A654-79EA-400B-9DE6-84C8E11394B4}";
  24.     extraqs += "&parentcustomeridname=My Account Name";
  25.     extraqs += "&parentcustomeridtype=account";
  26.     
  27.     //Set Standard Text Fields
  28.     extraqs += "&firstname=John";
  29.     extraqs += "&lastname=Smith";
  30.     extraqs += "&mobilephone=1234567890";
  31.     extraqs += "&emailaddress1=mail@domain.com";
  32.  
  33.     //Set Features & Open new Window/CRM Form
  34.     var features = "location=no,menubar=no,status=no,toolbar=no";
  35.     var newWindow = window.open("/main.aspx?etn=contact&pagetype=entityrecord&extraqs=" +
  36.     encodeURIComponent(extraqs), "_blank", features, false);
  37.     
  38.     //Optionally - you can interact with the Window e.g. Resize it etc...
  39.     newWindow.resizeTo(528, 500);
  40. }

No comments:

Post a Comment

Action Microsoft.Crm.Setup.Common.Analyzer +CollectAction failed. Fatal error during installation

When installing the Srs Data Connection (Microsoft Dynamics CRM Reporting Extensions), you may have experienced the following error: ...