As Power Apps (Model-Driven Apps) developers, often we face this scenario of triggering a change of value in one field when a value in another field is modified. In this blog we are going to see how to apply OnChange event in a model driven app field to trigger values in another field.
- First of all, create a solution and in that solution create a table or add any existing table (here I have created a new table named TestOnChange). Then go to the table and select form options.
2. In Forms option there are multiple form designs but for our case we will use the main one which is usually used for create new records or edit record.
3. Edit the form and you will be on the following screen. From the right-side panel select event tabs and click on Add Library.
4. This will let you include already added JavaScript file which contains the OnChange event code or you can add a new one. In our case we will add a new file in which the logic is written.
Note: You can also add this file under web resources of your solutions before adding from this dialog.
5. The code in the JavaScript file is below where getAttribute refers to the logical name of the field.
Note: To check the logical name of the field you can go to the following tab.
6. After adding the file you will see OnChange option on the event tab. Select the field first on which you will apply the event and click on Event Handler.
7. Set the following properties. Function is the name of the function defined in the JavaScript file which will be called on OnChange event.
8. Once done select the table field which will trigger this OnChange event.
9. Once all steps are done just save and publish your app. Now whenever the Name values is changed the OnChange trigger will update the value in FirstName field as defined the JavaScript function.