In Dynamics 365, it is common to extend the application’s capabilities to perform specific actions based on complex business logic. The most common way to do this is by utilizing JavaScript Functions in a web resource file.
Summary of the key syntax
Here is a high-level summary of the main Microsoft Dynamics 365 JavaScript syntax covered in this blog post.
Get the form contextexecutionContext.getFormContext();
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 formexecutionContext.getEventArgs().preventDefault();
Check if the default save behavior was inhibitedexecutionContext.getEventArgs().isDefaultPrevented();
Here are the steps to perform the JS code to perform the functions:
Step1: Goto solution;
Step2: Create JS in VS;
Step3: Add library to solution and publish
Step4: Goto the form account form editor
Step5: Select the event;
Step6: Add the method passing argument execution context
As every developers are aware of Dynamics 365 JS functionality is event-driven trigger based snippet, the even types are falling into 3 categories: OnSave, OnLoad & OnChange. Below are several examples are your reference:
//Onload events:
function Onload(executionContext){
try{
//Get the form context
var formContext = executionContext.getFormContext();
//Sample code for Onload Event
Xrm.Utility.alertDialog(“Good evening and Welcome to XRM with JS”);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
//Execute all OnSave events
function OnSave(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Sample code for OnSave Event
Xrm.Utility.alertDialog(“This is an alert for OnSave Event”);
}
catch(e) {
Xrm.Utility.alertDialog(e.message);
}
}
//Execute Field on Change events here, this could be specific to each field
function OnChange(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Sample code for on Change events
Xrm.Utility.alertDialog(“This is an alert for on Change Event.”);
}
catch(e) {
Xrm.Utility.alertDialog(e.message);
}
}
//Get Lookup ID
function GetEntityLogicalName(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Get entity logical name, give lookup field logical name
var entityName = formContext.getAttribute(“ccc_booking”).getValue()[0].entityType;
Xrm.Navigation.openAlertDialog(entityName);
}
catch(e) {
Xrm.Navigation.openAlertDialog(e.message);
}
}
For more methods, you can find my github URL: https://github.com/jerryyangy/most-used-JavaScript-Methods-in-D365