In this blog post, I will explain what is the save event in Dynamics 365 CRM, how to use JavaScript to call custom functions on form save, why do we need to add a custom logic there, and how to use the formContext.data.entity.addOnSave() method to register a function for the save event. I will also provide a use case and a code example for this method.
Understanding the Save Event in Dynamics 365
The save event in Dynamics 365 CRM is triggered when a user or a script tries to save a record in a form. The save event can be synchronous or asynchronous, depending on the configuration of the form. The save event allows us to perform some validations, calculations, or other actions before the record is saved to the database.
Why Customize the onSave Function
Sometimes, we may need to add a custom logic to the save event, for example, to check some conditions, update some fields, or call an external service.
To do this, we can use the formContext.data.entity.addOnSave() method, which registers a function that will run when the save event occurs. The function will receive an execution context object as a parameter, which contains information about the event and the form.
One use case for this method is to validate some fields before saving a record. For example, suppose we have a custom entity called Project, which has a field called Budget. We want to make sure that the Budget field is not negative or zero, and if it is, we want to cancel the save operation and show an error message. To achieve this, we can write a function like this:
function validateRequiredField(executionContext) {
// Get the form context
var formContext = executionContext.getFormContext();
// Get the value of the required field
var fieldValue = formContext.getAttribute(“new_requiredfield”).getValue();
// Check if the required field is populated
if (!fieldValue) {
// Display an error message
formContext.ui.setFormNotification(“Please populate the required field before saving.”, “ERROR”);
// Prevent the save operation
executionContext.getEventArgs().preventDefault();
}
}
Then, we can register this function for the save event using the formContext.data.entity.addOnSave() method. We can do this in the form OnLoad event handler, or in any other place where we have access to the form context. For example:
// Attach the custom function to the onSave event
formContext.data.entity.addOnSave(validateRequiredField);
In this example, the validateRequiredField function is defined to check whether the specified field (new_requiredfield) is populated. If the field is empty, an error message is displayed, and the save operation is prevented. This ensures that the required field is filled before the record is saved, maintaining data accuracy and adhering to business rules.
Conclusion
The onSave function in the Dynamics 365 client API provides a lot of options for customization and expansion. By grasping the save event, incorporating custom logic, and exploring practical scenarios, developers can enhance the Dynamics 365 experience for their organizations. They can use this function to enforce business rules, activate workflows, and apply advanced validations, giving them the ability to customize Dynamics 365 to fit their business processes.
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