Model Driven Apps (Part 3)
Neil Haddley • October 8, 2023
JavaScript to update a Business Process Flow Stage
JavaScript can be used to extend model driven apps.
It is possible to configure model driven apps to call a JavaScript function:
When a form has loaded (OnLoad)
When a user make a change to a control (OnChange)
When a user saves a record (OnSave); and
When a user clicks a button
The JavaScript function called might:
Validate user input
Show or Hide part of the form
Update a Business Process Flow
Display a Notification; or
Create a new Record
See also: Power Apps Component Framework
Xrm.Page.data.process.moveNext() and Xrm.Page.data.process.movePrevious() can be used to update a Business Process Flow Stage.

const process = Xrm.Page.data.process

process.moveNext()

process.moveNext()

process.movePrevious()
JavaScript to Show or Hide a Tab
Xrm.Page.data.ui.tabs.get("<tab name>").setVisible(false) and Xrm.Page.data.ui.tabs.get("<tab name>").setVisible(true) can be used to hide or show a tab.

Xrm.Page.ui.tabs.get('{f3ca5bbd-8896-40c1-9cb2-bc5a744ac9f7}').setVisible(true)

Xrm.Page.ui.tabs.get('{f3ca5bbd-8896-40c1-9cb2-bc5a744ac9f7}').setVisible(false)
JavaScript to Read and Update Field/Control/Attribute Values
Xrm.Page.getAttribute('nh_businessneed').getValue() and Xrm.Page.getAttribute('nh_businessneed').setValue('Hello') can be used to read or update a field/column/attribute value.

Xrm.Page.data.entity.attributes._collection to view attribute namesXrm.Page.getAttribute('nh_businessneed').getValue()

Xrm.Page.getAttribute('nh_businessneed').setValue('Hello')
JavaScript to clone a record

Clone the current Project table record
Configuring a model driven app to call a JavaScript function
I added a Clone button to a model driven app's form command bar and used JavaScript to implement the record copying functionality
Notice that in the Modern Command Designer the execution context is not passed to a JavaScript handler function as a parameter.

This Clone function expects to be passed the primaryControl

This JavaScript file is uploaded (and updated) as a Web resource

After every update the Web resource should be published

I added a Clone button

I clicked the clone command and generated a new record