In this post, I’m going to talk about how to use the formContext.getAttribute(arg).getIsDirty() web API to check if there are unsaved changes in the form fields using JavaScripts. This is useful when you want to perform some actions based on the dirty state of the column, such as showing a warning message, enabling or disabling a button, or validating the input.
How does getIsDirty() work?
The formContext.getAttribute(arg).getIsDirty() web API returns a boolean value indicating if there are unsaved changes to the column value. An unsaved change to a column value means the client value is different from the last known committed value retrieved from Dataverse by the client from runtime.
For example, if you edit a text field on a form and then click on another field without saving, the text field will have an unsaved change and the getIsDirty() method will return true for that attribute.
To use the getIsDirty() method, you need to pass the name of the attribute as an argument. For example, if you want to check if the name field of an account record has unsaved changes, you can use this code:
var nameAttribute = formContext.getAttribute("name");
var isNameDirty = nameAttribute.getIsDirty();
The isNameDirty variable will store either true or false depending on the dirty state of the name attribute.
You can also use the getIsDirty() method for other types of attributes, such as lookup fields, option sets, date and time fields, etc. The only exception is the composite address fields, which do not support this method. For composite address fields, you need to use the getIsDirty() method for each individual component of the address, such as street1, city, country, etc.
How to use the getIsDirty() method for different types of attributes:
Check if the primary contact lookup field has unsaved changes:
var primaryContactAttribute = formContext.getAttribute("primarycontactid");
var isPrimaryContactDirty = primaryContactAttribute.getIsDirty();
Check if the account rating option set field has unsaved changes:
var ratingAttribute = formContext.getAttribute("creditlimit");
var isRatingDirty = ratingAttribute.getIsDirty();
Check if the created on date and time field has unsaved changes:
var createdOnAttribute = formContext.getAttribute("createdon");
var isCreatedOnDirty = createdOnAttribute.getIsDirty();
You can use these boolean values to perform various actions based on the dirty state of the column.
For example, you can show a warning message to the user if they try to navigate away from the form without saving the changes. You can also enable or disable a button depending on whether there are unsaved changes or not. You can also validate the input and prevent saving if the column value does not meet certain criteria.
Here are some examples of how to perform these actions using the getIsDirty() method:
Show a warning message if there are unsaved changes to the name field:
if (isNameDirty) {
Xrm.Navigation.openAlertDialog({ text: "You have unsaved changes to the name field. Please save before leaving." });
}
Enable or disable a button based on the dirty state of the rating field:
if (isRatingDirty) {
Xrm.Page.ui.controls.get("myButton").setDisabled(false);
} else {
Xrm.Page.ui.controls.get("myButton").setDisabled(true);
}
Validate the input and prevent saving if the created on field has unsaved changes and is in the future:
if (isCreatedOnDirty) {
var createdOnValue = createdOnAttribute.getValue();
var currentDate = new Date();
if (createdOnValue > currentDate) {
Xrm.Navigation.openAlertDialog({ text: "The created on date cannot be in the future." });
context.getEventArgs().preventDefault();
}
}
In conclusion, the formContext.getAttribute(arg).getIsDirty() web API is a handy tool to check if there are unsaved changes to the column value of a record. You can use it for various scenarios and customize your form behavior accordingly. I hope you found this post useful and learned something new. Thanks for reading!
For more in-depth insights and tutorials on Programming or Dynamics 365 features and customization, check out our posts to further enhance your proficiency and maximize the potential of your skills.
Leave a comment