Skip to main content

JavaScript: Most Commonly Used JavaScript Functions in Dynamics 365

Welcome to my blog! In this post, we will dive into some of the most commonly used JavaScript functions when developing client-side scripts for Dynamics 365. JavaScript is essential for enhancing user experience, automating processes, and interacting with form elements in Dynamics 365. Whether you're customizing forms or developing business logic, these functions will be extremely useful in your daily development.


If you find this post helpful, please feel free to follow and support me on my LinkedIn profile. Let's get started!


1. formContext.getAttribute()


In modern Dynamics 365, formContext is used to interact with form elements instead of Xrm.Page. To get the value of an attribute (field), we use formContext.getAttribute().


Syntax:


var attributeValue = formContext.getAttribute("fieldname").getValue();


Use case: Retrieving the value of a contact's "Email" field.


var email = formContext.getAttribute("emailaddress1").getValue();



2. formContext.getControl()


Similar to the getAttribute() method, getControl() allows you to access a control on the form, like a textbox, dropdown, or lookup.


Syntax:


var control = formContext.getControl("fieldname");


Use case: Hiding a field control based on a condition.


var emailControl = formContext.getControl("emailaddress1");

emailControl.setVisible(false);



3. formContext.data.entity.save()


The save() function allows you to save the current record. It's commonly used when you want to explicitly save the record after a custom validation or process.


Syntax:


formContext.data.entity.save();


Use case: Saving the record after a custom validation.


if (formContext.getAttribute("emailaddress1").getValue() != null) {

    formContext.data.entity.save();

}



4. formContext.getAttribute().setValue()


This method lets you set a value for an attribute dynamically. This is useful for updating fields based on user interaction or custom logic.


Syntax:


formContext.getAttribute("fieldname").setValue(value);


Use case: Setting a default value for a custom field based on another field's value.


var contactType = formContext.getAttribute("contacttype").getValue();

if (contactType === "VIP") {

    formContext.getAttribute("discount").setValue(20);

}



5. formContext.data.entity.getId()


This function allows you to retrieve the ID of the current record. It's useful when you need to work with the unique identifier of the record in your JavaScript logic.


Syntax:


var recordId = formContext.data.entity.getId();


Use case: Getting the ID of the current contact record for custom logic.


var contactId = formContext.data.entity.getId();



6. Xrm.Utility.alertDialog()


This function allows you to show an alert message to the user, which is helpful for notifying users of important information or errors.


Syntax:


Xrm.Utility.alertDialog("This is an alert message!");


Use case: Displaying a warning message when a mandatory field is not filled.


var email = formContext.getAttribute("emailaddress1").getValue();

if (!email) {

    Xrm.Utility.alertDialog("Email address is required!");

}



7. formContext.ui.setFormNotification()


This function is used to display a notification on the form. It’s commonly used for informing the user of success, warnings, or errors in the process.


Syntax:


formContext.ui.setFormNotification("Your custom message", "INFO", "uniqueId");


Use case: Displaying a success notification after saving the record.


formContext.ui.setFormNotification("Record saved successfully", "INFO", "saveSuccess");



8. formContext.ui.clearFormNotification()


If you want to remove a previously set notification, clearFormNotification() is the function to use. It helps you remove old messages from the form.


Syntax:


formContext.ui.clearFormNotification("uniqueId");


Use case: Clearing a form notification after the user resolves an issue.


formContext.ui.clearFormNotification("saveSuccess");



9. formContext.ui.tabs.get("tabname").setVisible()


This function is used to hide or show tabs dynamically based on certain conditions. It helps in customizing the form’s layout based on business rules.


Syntax:


formContext.ui.tabs.get("tabname").setVisible(true/false);


Use case: Hiding a tab based on the record type.


var accountType = formContext.getAttribute("accounttype").getValue();

if (accountType === "Prospect") {

    formContext.ui.tabs.get("details").setVisible(false);

}



Conclusion


As Dynamics 365 evolves, it is essential to stay updated with the best practices for client-side scripting. Using formContext instead of Xrm.Page is a crucial step in improving the performance and scalability of your JavaScript code. Familiarizing yourself with these commonly used functions will empower you to create dynamic and responsive forms that provide an excellent user experience.


I hope this post was insightful. Feel free to follow me on LinkedIn for more updates and tips on Dynamics 365 development.


https://in.linkedin.com/in/pavan-kumar-vuyyuru

Comments

Popular posts from this blog

Powerapps: Collections in Canvas Apps | Collect, ClearCollect, Clear in Powerapps

Collections : Collections are special types of variables or data storage mechanism which can be used to manipulate the data within Power Apps.  Collections are usually a group of similar items which acts like a table of values with columns and rows.  We can create, update, delete, use it as the data source for a gallery to view the items within Power App.  Collect, Clear, and ClearCollect functions:   Collect : In Power Apps, the Collect function is used to create or update a collection in your app.  Collections are temporary data sources that allow you to store and work with data within your app.  The Collect function is versatile and can be used in various scenarios to add, modify, or remove records from a collection.  Syntax : Collect( IceCream, { Flavor: "Pistachio", Quantity: 40 }, { Flavor: "Orange", Quantity: 200 } )   Adds two records to the IceCream collection that includes a quantity of pistachio and orange ice cream.  Clear : In Po...

Understanding the Basics of C# Plugins in Dynamics 365: A Simple Account Update Example

Welcome to my blog! Today, we’ll dive into the world of C# plugins in Dynamics 365. If you're working with Dynamics 365, you’ve likely heard of plugins, which allow you to extend the platform's functionality and automate various processes. In this post, I’ll walk you through the parts of a plugin, using a simple example of updating an account record. What Is a C# Plugin? A plugin in Dynamics 365 is a custom business logic component that responds to specific events in the system. Plugins are executed in response to a trigger event, like creating or updating a record, and can be used to modify data or integrate with other systems. Written in C#, they are powerful tools for customizing and extending the functionality of Dynamics 365. The Structure of a C# Plugin A plugin typically consists of several parts: Plugin Registration – You register the plugin in Dynamics 365 to define what event triggers the plugin (create, update, delete) and which entity it will affect (e.g., A...

Essential Dynamics 365 Plugins for Sales & Customer Service

Introduction In Dynamics 365, plugins automate business processes, enforce rules, and ensure data integrity. Whether in Sales or Customer Service , plugins help streamline workflows by executing logic at the right time in the pipeline. This post covers key plugin scenarios , detailing: ✅ Execution pipeline stage (Pre-Validation, Pre-Operation, Post-Operation) ✅ Synchronous or Asynchronous execution ✅ Pre-Image / Post-Image usage ✅ Rollback handling Rollback Handling in Plugins When Does a Plugin Rollback? A plugin automatically rolls back if: An exception is thrown in a synchronous plugin. A transaction fails in a Pre-Validation or Pre-Operation stage. How to Handle Rollbacks? Use Try-Catch Blocks : Prevent unwanted failures by catching errors and logging details. Throw an Exception When Needed : Stop invalid data changes (e.g., prevent closing an Opportunity without a Quote). Compensating Logic in Async Plugins : Since Post-Operation Async plugins do not par...