CRM Crate

How to automatically change a Form using JavaScript in Dynamics 365 CRM?

We will learn to automatically change a Form using JavaScript in Dynamics 365 CRM. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Dynamics 365.

How to automatically change a Form using JavaScript in Dynamics 365 CRM?

What is an entity form in Dynamics 365?

With Dynamics 365 CE, forms provide the user interface that people use to interact with the data they need to do their work. Below are the different types of forms in Dynamics 365 CE.

Form TypeForm Description
MainUsed in the Dynamics 365 Customer Engagement (on-premises): the Dynamics 365 for Customer Engagement web application, Dynamics 365 for Outlook, and Dynamics 365 for tablets.
These forms provide the main user interface for interacting with entity data.
Quick CreateUsed in Dynamics 365 Customer Engagement (on-premises), the Dynamics 365 for Customer Engagement web application, Dynamics 365 for Outlook, and Dynamics 365 for tablets.
For updated entities, these forms provide a basic form optimized for creating new records.
Quick ViewUsed in Dynamics 365 Customer Engagement (on-premises), the Dynamics 365 for Customer Engagement web application, Dynamics 365 for Outlook, and Dynamics 365 for tablets.
For updated entities, these forms appear within the main form to display additional data for a record that is referenced by a lookup field in the form.
CardUsed in views for Unified Interface apps. Card forms are designed to present information in a compact format that is suitable for mobile devices.

How can we switch between the forms in Dynamics 365 CRM?

One entity can have multiple forms associated with it and the users can switch between these forms using the out of the box conventional method as shown below.

Other than this conventional method, we can also change the forms programmatically / dynamically using JavaScript as shown below.

Using navigate client API for dynamically changing the forms in D365

We will use the “navigate” client API for programmatically switching between multiple forms as per the business conditions.

formItem.navigate();

Opens the specified form.
When you use the navigate method while unsaved changes exist, the user is prompted to save changes before the new form can be displayed. The Onload event occurs when the new form loads.

JavaScript code for automatically changing the forms in Dynamics 365

The below JavaScript function is triggered during the “OnChange” event of the form’s field “Type Of Form”.

//CRM Crate JavaScript Function
function CallMethod (executionContext) {

//Generating Form Context
var formContext = executionContext.getFormContext();

//Retrieving Field Data
var FieldValue = formContext.getAttribute("cc_typeofform").getText();

ChangeForm(FieldValue, formContext);
}

function ChangeForm(FieldValue, formContext) {

//Retrieve Current Form
  var currentForm = formContext.ui.formSelector.getCurrentItem();
  
//Retrieve Available Forms
  var availableForms = formContext.ui.formSelector.items.get();
  if (currentForm.getLabel().toLowerCase() != FieldValue.toLowerCase()) {
	  
	  //Looping Through Available Forms
    for (var i in availableForms) {
      var form = availableForms[i];
      if (form.getLabel().toLowerCase() == FieldValue.toLowerCase()) {
		  
	//Programmatically Changing The Form Using "navigate" Client API
        form.navigate();	
        return true;
      }
    }
  }
}

Validate the functionality in Dynamics 365

Once the above JavaScript function is deployed, navigate to the Dynamics 365 and validate the functionality as shown below.

Thus, we learned to automatically change a Form using JavaScript in Dynamics 365 CRM.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.