Adventures with Power Platform – Short Circuit Model-Driven App formOnSave Event

Let’s say you have a Dataverse record that you want to do some things to post creation, for example, maybe you have special requirements for generating a value for the Primary Name column or need to add records to another Dataverse table, and so on.

That begs the question, how would you prevent the user from making changes to the Dataverse record before it was ready?

I created a Table in Dataverse called Item, with the following additional fields:

Description – Text

State – Internal choices for Preparing, Error and Ready

State Reason – Text with an explanation of the State, if needed, for example, what if one of the steps in the things I wanted to do was unable to complete, this is where I would put that message.

State defaults to Preparing.

I created a Flow called Item Row Added to mimic doing some work whenever a new record was added to the Item table.

Figure 1 – Flow that mimics doing “work”

I created a Model-Driven App called Item App and added support for the Item table.

I add the Description, State and State Reason fields to the Information Main Form. I set State and State Reason to Read-only.

Figure 2 – Added additional fields, marked State and State Reason as Read-only

Let’s look at the requirements again.

If the record is new, allow the User to Save the changes.

If the record is an existing record, and is in any other State than Preparing, allow the User to Save the changes.

To make this work I was going to have to use JavaScript, so I created a file called ItemForm.js with thhe code found at https://gist.github.com/mattruma/02d9b9f3c82c61ac3911bf36ab2f4399#file-itemwithstateform-js.

Note, my publisher prefix is maruma, so if you tried this, would be whatever your publisher prefix is for accessing fields in the Item table.

Form the Solution, I uploaded the JavaScript file as a Web Resource by clicking on New and then Web Resource.

Figure 3 – Upload the JavaScript file as a Web Resource

With the JavaScript file uploaded time to wire up the JavaScript to the Information Main Form.

From the Solution, I click on the three dots next to my Model-Driven App and click Edit.

From the Sidebar Menu I click Pages and then Item forms.

I then hover over the Information Main Form and click the Pencil icon.

Figure 4 – Edit the Information Main Form

Time to wire up the events.

I click the Events tab on the far right and then click Event Handler for On Save.

I set Library to my JavaScript file which I uploaded as a Web Resource.

Figure 5 – Wire-up On Save event to the JavaScript function

I set Function to my ItemForm.formOnSave function in my JavaScript file and check Pass execution context as first parameter.

Repeat the same for On Load using the ItemForm.formOnLoad function.

Click Save and publish when complete.

Click Back and then click Publish to publish the application.

Click Play.

Now when I run my app and look at a record that has a State of Preparing a notification and an alert are displayed.

Figure 6 – Display notification if not a new record or State is Preparing

Same dialog will show if the User clicks Save or Save and Close.

There we go!

Not the most “low-code” solution, but it gets the job done!


Discover more from Matt Ruma

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *