Adding JavaScript to Quick Create Forms in CRM 2015

Disclaimer: This blog is for information purposes ONLY and contains unsupported code that is subject to break during major CRM updates.

Microsoft Dynamics CRM 2015 features a new Quick Create form. One of the enhancements that come with the new Quick Create form is that users can now add JavaScript to the form exclusively. In today's blog, we will delve into the new Quick Create form in more detail and we will also outline the steps needed to add JavaScript to the form. This will help users utilize the new Quick Create form features more effectively. Let's get started!

Note: Experience with creating JavaScript web resources is helpful in completing this solution.

There are multiple places within CRM where the Quick Create form can be used:

A. The main application ribbon on the top of your window.

Adding JavaScript

B. From the +New button found on the drop-down menu on an entity form.

Adding JavaScript

C. From the + button on sub-grids (and associated views when adding a new item).

Adding JavaScript

In this post, we will be focusing on certain parameters that are available for item B as described above. This is generally helpful when you have specific JavaScript to run against specific look-ups on entity forms. In this example, we want to set a specific field on the contact record depending on which +New button is clicked on the Case form. We can do this on the Quick Create form's on-load event and use some parameters that are available from the parent window calling the Quick Create form. There are two parameters that are available on the Quick Create form when called from a look-up control (CreateFromId and CreateFromType) that will determine which entity/record it is being called from. These are available as part of the Xrm.Page.context.getQueryStringParameters() from the Quick Create form onload.

Below is the sample code for setting the ParentCustomerId and Customertypecode fields on the Quick Create contact form when the +New button is clicked:

function Form_Onload() {

var contextParams = Xrm.Page.context.getQueryStringParameters();

 

if (contextParams._CreateFromType != null && contextParams._CreateFromType == 112) {

var contactLookup = top.document.getElementById('new_contact_i_lookup_quickcreate');

var customerLookup = top.document.getElementById('customerid_i_lookup_quickcreate');

var contentFrame = top.document.getElementById('contentIFrame0');

 

if (contentFrame != null) {

 

var clientLookup = contentFrame.contentWindow.Xrm.Page.getAttribute("new_account");

if (clientLookup != null) {

var clientValue = clientLookup.getValue();

if (clientValue != null) {

Xrm.Page.getAttribute("parentcustomerid").setValue(clientValue);

}

}

if (customerLookup != null) {

Xrm.Page.getAttribute("customertypecode").setValue(100000000); //Customer

}

if (contactLookup != null) {

Xrm.Page.getAttribute("customertypecode").setValue(100000001); //Contact

}

}

}

}

After you use the code above on the Quick Create form of contact, try creating a new contact from the Case's customerid look-up. This will set the new_account (Account look-up) from the case form to the ParentCustomerId (Parent Customer) field on the Quick Create form. You will also want to set the Customertypecode (relationship type) field on the contact create form to Customer as shown in the steps below:

1. Once an Account is selected, the customer look-up +New button is clicked.

Adding JavaScript

2. Next, the Quick Create form opens with the Parent Customer set to Blue Yonder Airlines (sample) and Relationship Type set to Customer.

Adding JavaScript

3. Similarly, when the Contact look-up +New button is clicked, the Customer Name and Relationship Type (=contact) are populated accordingly.

Adding JavaScript

Adding JavaScript

To explain the code, this if statement determines that the quick create contact form is being called from the case form: if (contextParams._CreateFromType != null && contextParams._CreateFromType == 112)

The two lines of code below will get the contact look-ups that are initiating the Quick Create form within the case form itself:

var contactLookup = top.document.getElementById('new_contact_i_lookup_quickcreate');

var customerLookup = top.document.getElementById('customerid_i_lookup_quickcreate');

Note: This particular code is accessing the DOM and hence is un-supported. However, it will do the trick in CRM 2015! The first part of the element name is the CRM field name, but this might be different if the field is added twice in the form. CRM tends to create the name as new_contact1 (for the new_contact field) if it is added twice on the form.

Based on whichever element is available, you can set the Relationship Type on the opened Quick Create form.

Finally, use this last piece of code to set the Parent Customer depending on the Case form's Account field value:

var clientLookup = contentFrame.contentWindow.Xrm.Page.getAttribute("new_account");

if (clientLookup != null) {

var clientValue = clientLookup.getValue();

if (clientValue != null) {

Xrm.Page.getAttribute("parentcustomerid").setValue(clientValue);

}

}

This is more straight-forward in that we are getting it from the main content frame window which has the XRM object and can get any field values from the form itself.

We hope you found today's blog helpful! These little tricks will come handy in various situations in terms of utilizing the new Quick Create forms more productively. To keep up to date on more tips and tricks like this one, be sure to subscribe to our blog!

Until next time, happy CRM'ing!

How to: Open Quick Create from a Sub Grid

The Quick Create Forms introduced in Dynamics CRM 2013 are very helpful in capturing the required information for a record without navigating away from the current screen. The Quick Create button is available in the Navigation Bar, thereby allowing the user to click it from anywhere in CRM. The Quick Create form opens without overtaking the current window. From CRM, the user can now open the quick create form from the Create button in the Navigation bar (as shown below) or the New button available in the lookup search from sub grids, provided of course, that the entity is configured for Quick Create.

How to: Open Quick Create from a Sub Grid

 

Opening a Quick Create Form from the sub grid is very useful as it does not disturb the current window while allowing the user to add a new record. However, to do so the user must to click in the + sign above the sub grid and then click on New. To reduce the number of clicks CRM can be configured to open the quick create form directly on the click of the + sign!

For example, consider two entities named Parent and Child with an 1:N relationship between them. The parent entity form has a sub grid of the child entity. On click of the + sign in the sub grid we will configure CRM to open the Child quick create directly.

There are three possible ways to accomplish this:

Make the parent lookup a required field

In the child entity set the parent lookup's Field Requirement to Business Required. Now if you press the + sign in the sub grid on the parent form the child quick create form opens up as shown below

How to: Open Quick Create from a Sub Grid

The reason this is happening is because the + button in the sub grid is a combination of the Add Existing and Add New button. If the parent look up is mandatory on the child form the functionality for add existing will not be invoked as CRM assumes all the child records will have an associated parent and so it will directly call the add new functionality.

Hide the Add Existing button for all entities

If for some business related reason the parent lookup field in the child entity cannot be made mandatory, then the quick create form can be opened from the sub grid by customizing the sub grid ribbon as shown below. For convenience Ribbon Workbench is used here, but it can also be achieved by changing the customizations.xml file of the solution containing the child entity. Open the child entity in Ribbon Workbench and right click on the Add Existing button and press Hide Button. After publishing the solution try clicking the + sign in the child sub grid, it will open the Quick Create Form.

 

How to: Open Quick Create from a Sub Grid

Hide the Add Existing button for a single entity

The above change will affect all the current and future child entity sub grids added on any CRM forms. If the above functionality has to be achieved for a specific entity it can be very easily achieved by adding a Display Rule to the Add Existing button on the Child Entity Sub Grid. The display rule will contain a FormEntityContextRule as shown below. This will open the Child Entity quick create form only from the sub grid on the Parent Entity form and not others.

How to: Open Quick Create from a Sub Grid

There you have it! You are now full of CRM mastery! To become even more of a Dynamics CRM genius, head over to our main blog page for all sorts of wonderful CRM training and information!

Happy CRM'ing!

Dynamics CRM 2015 Introduces Nested Quick Create Forms

Ready for another neat new feature of Microsoft Dynamics 2015? There is now an improvement to the already cool Quick Create Forms introduced in 2013! What's new you ask? Let's take a look…

Say you go to open the Quick Create form to create a new Contact record, only to realize that the Account is not already in CRM.

Dynamics 2015 Introduces Nested Quick Create Forms

No worries, simply click on the New (+) icon to open the Account Quick Create form.

Dynamics 2015 Introduces Nested Quick Create Forms

Add your new Account and be on your way! Less clicks (hurray!) and you no longer need to access the full form to enter the Account information as you did in 2013.

Dynamics 2015 Introduces Nested Quick Create Forms

If you have found this useful, we have a plethora of super helpful Dynamics CRM blogs to assist with any other questions you might have!

Happy CRM'ing!

 

Friday Flash Fix: Open a Quick Create Form using Xrm.Utility

The Xrm.Utility provides functions that can be used in client side scripting. In CRM 2015 Update 1, one of these functions is openQuickCreate. This function is used to open a new quick create form in CRM.

It can be called as below:

Xrm.Utility.openQuickCreate(entityLogicalName,createFromEntity,parameters).then(successCallback, errorCallback);

entityLogicalName – Logical name of an entity like an account, contact etc. It is a required parameter.

createFromEntity - Lookup parameter that provides default values based on mapped attribute values.

parameters – Querystring parameters used to populate the values on the newly created form.

successCallback – Call back function called on successful creation of record.

errorCallback – Function called when the operation fails.

var objCurrentAccount= {
    entityType: "account",
    id: Xrm.Page.data.entity.getId()
};
var callback = function (obj) {
    console.log("Created new account with id:" + obj.savedEntityReference.id);
}
var objChildAccount= {
 name: Xrm.Page.getAttribute("name").getValue(),
 telephone1:Xrm.Page.getAttribute("telephone1").getValue(),
 fax:Xrm.Page.getAttribute("fax").getValue()
 };
Xrm.Utility.openQuickCreate("account", objCurrentAccount, objChildAccount).then(callback, function (error) {     console.log(error.message); 
});

That’s all for the blog today! Check out our list of Dynamics CRM events to find a training or CRM Boot Camp near you!

Happy CRM’ing!