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
Post a Comment