Microsoft Dynamics 365 offers the ability to vary the behavior of forms based on the form type and how the form is saved, using JavaScript. In this blog post, we cover how we can make use of this flexibility offered by the platform to deliver unique solutions for end users.
Summary of the key syntax
Here is a high-level summary of the main Microsoft Dynamics 365 JavaScript syntax covered in this blog post.
- Getting the type of form:
formContext.ui.getFormType();
- Hide a tab on a form (where “TabName” represents the name of the tab you would like to hide):
formContext.ui.tabs.get("TabName").setVisible(false);
- Get the object with methods to manage the Save event:
executionContext.getEventArgs();
- Get the form Save Mode:
executionContext.getEventArgs().getSaveMode();
- Inhibit the default save behavior on a form
executionContext.getEventArgs().preventDefault();
- Check if the default save behavior was inhibited
executionContext.getEventArgs().isDefaultPrevented();
- Get the form context
executionContext.getFormContext();
Form Type
To get the Form Type, you use the following JavaScript code:
formContext.ui.getFormType();
Here are the different Dynamics 365 form types and their respective JavaScript return values:
Form type | Return Value |
Undefined | 0 |
Create | 1 |
Update | 2 |
Read Only | 3 |
Disabled | 4 |
Bulk Edit | 6 |
Application Example: Varying form behavior on Form Type
In this application example, we will hide the Products and Services tab on the Create type form (with JavaScript Return Value = 1). However, it will be still be visible on all other applicable form types. This prevents users from adding information to the Products and Services tab before a Contact record has been created and saved. Therefore, the user must first create the Contact record (using the Create form type) and after the record has been created, the users will be presented with the Update form type (with JavaScript Return Value = 2) and will be able to access the Products and Services tab as well as its contents.
The Contact entity’s create form type will look similar to this (i.e. the Products and Services tab is hidden):
In contrast, the Update form type still shows the Products and Services tab.
Here is the code to executes the requirement above.
//Hide Products and Services tab on Create form function HideProductsServicesTab(executionContext) { //Get form context var formContext = executionContext.getFormContext(); //Get form type var formType = formContext.ui.getFormType(); //If formtype is Create, hide Products and Services tab on form if (formType == 1) { formContext.ui.tabs.get("productsandservices").setVisible(false); } //To see the form type return value in your browser console console.log("Form type = " + formType); }
Save Mode
To get the form’s Save Mode, you use the following JavaScript code:
executionContext.getEventArgs().getSaveMode();
Here are Dynamics 365’s different Save Modes and their respective JavaScript return values as well as the applicable entities.
Entity | Save Mode | Return Value |
All | Save | 1 |
All | Save and Close | 2 |
All | Deactivate | 5 |
All | Reactivate | 6 |
Send | 7 | |
Lead | Disqualify | 15 |
Lead | Qualify | 16 |
User or Team owned entities | Assign | 47 |
Activities | Save as Completed | 58 |
All | Save and New | 59 |
All | Auto Save | 70 |
Application Example: Varying form behavior on Save Mode
Here is an example of how you can vary the form’s behavior on the Save Mode.
//Inhibit auto save functionality on a form function InhibitAutoSave(executionContext) { var eventArgs = executionContext.getEventArgs(); //To see the Save Mode return value in your browser console console.log("Save Mode = " + eventArgs.getSaveMode()); //If save mode is 'Save and Close' or 'Auto Save', inhibit default behavior i.e. save if (eventArgs.getSaveMode() == 2 || eventArgs.getSaveMode() == 70) { eventArgs.preventDefault(); } //To see if the auto save behavior was prevented in your browser console console.log("Default behaviour prevented: " + eventArgs.isDefaultPrevented()); }
If you have some unsaved changes on a form, the code above inhibits the regular form auto save behavior as well as inhibits the regular Save and Close auto save behavior, that you ordinarily get when you navigate away from from a form with unsaved changes. Instead of auto-saving (i.e. the default behavior), if you try to navigate away from a form with an unsaved changes, the JavaScript code above will block the auto save behavior and offer you the pop up notification below. If you choose “OK”, you will loose all your unsaved changes, and if you choose “Cancel”, you will remain the same form. With the default auto save behavior inhibited, users have to manually save the form, by clicking on the Save button or using the keyboard shortcut: “Control + S”.
What if you want to inhibit the auto-save behavior throughout your Dynamics 365 organization (i.e. disable auto-save on all forms), instead of a specific form? It would be inefficient to implement the JavaScript code above on all forms across all your Dynamics 365 organization entities. An efficient way execute such a requirement (i.e. on all forms) is to:
- Go to Settings > Administration.
- Click on System Settings.
- Click on the General tab
- Set the Enable auto save on all forms option, to No.
- Click OK, in the footer of the window.