Mastering Dynamics 365 Form Types with getFormType()
What is getFormType()?
The getFormType() method returns a numeric value that identifies the current form type. It is commonly used in JavaScript to apply different logic depending on whether the form is new, existing, read-only, or opened for bulk editing.
Syntax
The syntax for using this method is straightforward:
var formType = formContext.ui.getFormType();
The method returns an integer value representing the form type.
List of Form Types
Here's a breakdown of the supported form types and their corresponding values:
- Value 0: Undefined - The form type cannot be determined. This scenario is uncommon but may occur during initializations or special form loading situations.
- Value 1: Create - The user is creating a new record (e.g., New Account, New Contact, New Lead). Quick Create forms also return this value.
- Value 2: Update - The user is editing an existing record. This is the most commonly used form type in Dynamics 365.
- Value 3: ReadOnly - The record is displayed in read-only mode, and users cannot modify data.
- Value 4: Disabled - The form is disabled, and editing is not allowed. This typically occurs due to security restrictions or system behavior.
- Value 6: BulkEdit - The form is opened through the Bulk Edit functionality, allowing multiple records to be updated simultaneously.
These are the supported form types in modern Dynamics 365 model-driven applications. Quick Create forms return the same value as Create (1).
FormType 0 – Undefined
Value: 0
Description: The form type cannot be determined. This scenario is uncommon but may occur during initializations or special form loading situations.
FormType 1 – Create
Value: 1
Description: The user is creating a new record. Examples include creating a new Account, Contact, or Lead. Quick Create forms also return value 1.
if (formContext.ui.getFormType() == 1) {
console.log("CreateForm");
}
FormType 2 – Update
Value: 2
Description: The user is editing an existing record. This is the most commonly used form type in Dynamics 365.
if (formContext.ui.getFormType() == 2) {
console.log("UpdateForm");
}
FormType 3 – ReadOnly
Value: 3
Description: The record is displayed in read-only mode, and users cannot modify data.
if (formContext.ui.getFormType() == 3) {
console.log("ReadOnlyForm");
}
FormType 4 – Disabled
Value: 4
Description: The form is disabled, and editing is not allowed. This typically occurs because of security restrictions or system behavior.
if (formContext.ui.getFormType() == 4) {
console.log("DisabledForm");
}
FormType 6 – BulkEdit
Value: 6
Description: The form is opened through the Bulk Edit functionality and allows multiple records to be updated simultaneously.
if (formContext.ui.getFormType() == 6) {
console.log("BulkEditForm");
}
Complete JavaScript Example
Here's a comprehensive JavaScript function demonstrating how to use getFormType() with a switch statement:
function GetFormType(executionContext) {
var formContext = executionContext.getFormContext();
var formType = formContext.ui.getFormType();
switch (formType) {
case 1:
alert("CreateForm");
break;
case 2:
alert("UpdateForm");
break;
case 3:
alert("ReadOnlyForm");
break;
case 4:
alert("DisabledForm");
break;
case 6:
alert("BulkEditForm");
break;
default:
alert("UndefinedForm");
}
}
Real-World Use Cases
The getFormType() method is invaluable for implementing conditional logic:
Hide Fields on Create Form
Requirement: Hide certain fields until the record is saved.
if (formContext.ui.getFormType() == 1) {
formContext.getControl("new_field").setVisible(false);
}
Lock Fields on Update Form
Requirement: Prevent changes to specific fields after record creation.
if (formContext.ui.getFormType() == 2) {
formContext.getControl("new_field").setDisabled(true);
}
Prevent Logic During Bulk Edit
Requirement: Avoid running unnecessary scripts during bulk updates.
if (formContext.ui.getFormType() == 6) {
return;
}
Best Practices
To ensure efficient and maintainable customizations:
- Use Form Type Checks: Always validate the form type before executing specific logic.
- Avoid Unnecessary Processing: Skip heavy JavaScript operations on Create and Bulk Edit forms when not required.
- Handle Bulk Edit Carefully: Remember that Bulk Edit forms behave differently than standard forms.
- Test Multiple Scenarios: Validate Create, Update, and ReadOnly forms separately to ensure correct behavior.
- Use FormContext: Prefer using the
formContextobject over deprecatedXrm.Pagemethods for modern development.
Benefits
Leveraging getFormType() offers several advantages:
- Dynamic Form Behavior: Execute logic precisely when and where it's needed.
- Improved User Experience: Present users with only relevant fields and actions based on the form's context.
- Better Performance: Avoid unnecessary processing, leading to a snappier application.
- Easier Maintenance: Centralize form-specific logic, making it simpler to update and manage.
- Enhanced CRM Customization: Build more intelligent and robust Dynamics 365 solutions.
Conclusion
The getFormType() method is an essential JavaScript function in Dynamics 365 that helps developers identify the current form state and apply business logic accordingly. By understanding all available form types and their return values, organizations can build more efficient, maintainable, and user-friendly CRM customizations.
Whether you're hiding fields, validating records, or controlling form behavior, getFormType() is one of the most useful methods available in the Dynamics 365 Client API for creating sophisticated user interfaces and workflows.