Blog
Master the .fireOnChange() Function in Dynamics Web API
In the world of Dynamics 365 and Power Apps, the .fireOnChange() function is a powerful tool for developers. This function is part of the Client API and is used to trigger the OnChange event for a specific column programmatically. This tool is integral to utilizing JavaScript web resources to their fullest potential. Let’s dive into what this function does, how it works, and some practical use cases.
What is the .fireOnChange() Function?
The .fireOnChange() function is used to simulate the OnChange event on a column. This means that any scripts or logic that are tied to the OnChange event of that column will be executed as if the user had manually changed the value of the column and then moved focus away from it.
Syntax
The syntax for using the .fireOnChange() function is straightforward:
formContext.getAttribute("columnName").fireOnChange();
Here, formContext is the context of the form where the column exists, and “columnName” is the logical name of the column you want to trigger the OnChange event for.
When to Use .fireOnChange()?
There are several scenarios where you might want to use the .fireOnChange() function:
- Data Validation: If you have custom validation logic tied to the OnChange event of a column, you can use .fireOnChange() to ensure that this logic is executed after setting the column’s value programmatically.
- Cascading Changes: When changes in one column should trigger updates in other columns, you can use .fireOnChange() to ensure that all dependent logic is executed correctly. This is particularly useful if you are setting a field programmatically with another JavaScript program, as Dynamics will not trigger OnChange events from other JavaScript to avoid infinite loops or other potential issues.
- Form Automation: In complex forms where multiple fields are interdependent, .fireOnChange() can help automate the process of updating related fields without requiring user interaction.
Practical Example
Let’s consider a practical example where we have a form with two columns: Total Amount and Tax. We want to automatically update the Tax column whenever the Total Amount changes.
First, we set the value of the Total Amount column programmatically:
formContext.getAttribute("totalamount").setValue(1000);
Next, we trigger the OnChange event for the TotalAmount column to ensure that any logic tied to this event is executed:
formContext.getAttribute("totalamount").fireOnChange();
By doing this, any scripts or calculations tied to the OnChange event of the Total Amount column will run, ensuring that the Tax column is updated accordingly.
Important Considerations
- Synchronous Execution: The OnChange event is synchronous, meaning it runs immediately and blocks other operations until it completes. Avoid using asynchronous code within OnChange events.
- Avoiding Infinite Loops: Be cautious when using .fireOnChange() to avoid creating infinite loops where the OnChange event triggers itself repeatedly.
Conclusion
The .fireOnChange() function is a versatile tool in the Dynamics Web API that allows developers to programmatically trigger the OnChange event for columns. By understanding how and when to use this function, you can create more dynamic and responsive forms in your Dynamics 365 and Power Apps applications.
Reach out to Beringer today!
Need assistance with custom form logic? Beringer Technology Group is here to help!
Beringer Technology Group, a Microsoft Solutions Partner for Business Applications, specializing in Microsoft Dynamics 365 and CRM for Distribution also provides expert Managed IT Services, Backup and Disaster Recovery, Cloud Based Computing, Email Security Implementation and Training, Unified Communication Solutions, and Cybersecurity Risk Assessments.