Table of Contents
In some scenarios, we may need to restrict users from saving records under specific conditions within a Model-Driven App. For example, when the record has certain status, or when record changes should only be made by users with specific roles.
This blog will show how to use JavaScript code to customize your Model-Driven Apps and to return an error when users try to save a record, if it meets a condition, this is useful if you want to restrict saving a record in model-driven apps when for example it has “X” status. Restrictions on forms built with JavaScript code are a client-side validation, so it will only happen in the actual form that the code is added to. This restriction will not apply to table level, this means if users have access to the Dataverse portal and can see the data, they would be able to edit the record regardless of the restriction we uploaded in the form.
If back-end restrictions are needed, then this can be achieved to either business rules or through Dataverse security roles and/or business units.
Solution description
To address the validation described above, a JavaScript function is needed to read the record status (for example) and cancel the save process if the current status doesn’t meet the conditions. The JavaScript is then uploaded as a web resource within the Dataverse environment and linked to the form events.
Step 1. Pre-requisites
Step 2. Writing JavaScript Code to Hide/Show Tab
For this simple customization, this is the code being used:
function preventSaveBasedOnStatus(executionContext) {
var formContext = executionContext.getFormContext();
var statusValue = formContext.getAttribute("pgi_recordstatus").getValue();
// Define the status values that should prevent saving
var forbiddenStatuses = [213870002, 213870003, 553420001];
if (forbiddenStatuses.includes(statusValue)) {
executionContext.getEventArgs().preventDefault(); // Prevents the save operation
formContext.ui.setFormNotification("Saving is not allowed once record is approved", "ERROR", "statusNotification");
} else {
formContext.ui.clearFormNotification("statusNotification");
}
}
Step 3. Uploading JavaScript code to Dataverse
Go to JS and then “Add Library”
Next, click on “+ Web resource”
In the next panel, add the code, file name and type and Save and Publish
Once saved, we need to select the resource we just uploaded. Use the search bar to find it faster, select it and then click on add
Step 4. Adding event handlers to trigger the code
Go to the form’s events and add a new event handler
Select the library, function name and enable the two checkboxes:
After this is done we can test and run the app to make sure all works properly!
Step-by-step video walkthrough
Watch the video below to see a step-by-step walkthrough by Graciela, where she shows the process of Model-Driven App form customization with JavaScript
This video shows a different code that is uploaded to the form, but the same process can be applied to any type of customization with JavaScript.
How our Power Apps Development services can help
Restricting users and preventing the save operation when a record meets certain criteria is just one of the many use cases where JavaScript customization can be really useful for Model-driven apps. Using our Power Apps and Power Platform consulting services, we have worked with many clients to create customizations in model-driven apps and Dataverse forms, contact us, we’ll be more than happy to have a conversation and see how we can be of help for your team.
