Data Bear

OnSave Power Apps: Pre-Save & Post-Save Events Guide

Power BI Sharing

When building model-driven apps in Power Apps, one of the most powerful customization options is the OnSave Power Apps event. This event lets you define exactly what happens before or after a record is saved in Dataverse, giving you control over field values, navigation, and user experience.

In this guide, we’ll explore how to:

  • Set up OnSave event handlers in Dataverse.
  • Differentiate between Pre-Save and Post-Save behaviors.
  • Use custom JavaScript to join field values automatically.
  • Implement navigation and field updates after a record is saved.

If you want to take your Power BI and Dataverse skills further, check out this Power BI Training with Data Bear.

The Problem: Automating Data Joins in Dataverse

Imagine you’re managing contractors in Dataverse. Each contractor record has:

  • First Name
  • Last Name
  • Contract Number

You also have a calculated field that should join these three values into a single string (e.g., "John Doe - 12345").

While you could use a Power Automate flow to update this field after saving, this approach introduces delays and potential re-triggers. Instead, you can prefill the field before saving, ensuring that the data is ready immediately and preventing redundant flow executions.

Setting Up OnSave Event Handlers

Dataverse forms allow you to configure event handlers for different stages of the save process.

Pre-Save vs. Post-Save
  • Pre-Save: Runs before the record is committed to Dataverse. Ideal for scenarios where you need to populate fields or validate data before saving.
  • Post-Save: Runs after the record has been successfully saved. Useful for tasks like redirecting users to another page.

Example: Use Pre-Save to join First Name, Last Name, and Contract Number into a single field. Use Post-Save to navigate users to a summary page after saving.

Integrating Custom JavaScript

To control OnSave behaviors, you’ll need a JavaScript web resource in your Dataverse environment. Here’s a simplified version of what the script might look like:

function checkSaveType(executionContext) {
    var eventArgs = executionContext.getEventArgs();
    var saveMode = eventArgs.getSaveMode();

    // Save Type 1 = Save, 2 = Save and Close
    if (saveMode === 1) {
        setValueOnSave(executionContext, "pwltp_contractorname", " - ", 
            "firstname", "lastname", "contractnumber");
    } else if (saveMode === 2) {
        setValueOnSave(executionContext, "pwltp_contractorname", " - ", 
            "firstname", "lastname", "contractnumber");
    }
}

function setValueOnSave(executionContext, fieldToSet, separator, ...fieldsToJoin) {
    var formContext = executionContext.getFormContext();
    var values = [];

    fieldsToJoin.forEach(field => {
        var attribute = formContext.getAttribute(field);
        if (attribute) values.push(attribute.getValue());
    });

    var joinedValue = values.filter(Boolean).join(separator || " - ");
    formContext.getAttribute(fieldToSet).setValue(joinedValue);
}

This script:

  1. Detects the save mode (Save vs. Save & Close).
  2. Joins specified fields (First Name, Last Name, Contract Number).
  3. Updates the contractor name field before saving.
Understanding Save Modes

Dataverse provides multiple save modes with unique numeric codes:

  • 1 → Save
  • 2 → Save & Close
  • 59 → Deactivate
  • 5 → Save & New

You can use these codes to trigger different behaviors based on the action the user takes.Understanding Save Modes

Implementing Post-Save Navigation

In some cases, you may want users to automatically navigate to another page after saving a record. This is best handled in the OnLoad event, not directly in OnSave.

Example:

function loaded(executionContext) {
    var formContext = executionContext.getFormContext();

    formContext.data.entity.addOnSave(function (context) {
        var eventArgs = context.getEventArgs();
        if (eventArgs.getSaveMode() === 1) {
            Xrm.Navigation.openUrl("/main.aspx?appid=<APPID>&pagetype=entitylist&etn=account");
        }
    });
}

This ensures that:

  • The contractor record saves successfully.
  • Users are redirected to the Accounts page immediately after saving.
Best Practices for OnSave Behaviors

Always pass the execution context to your functions.
Differentiate between Pre-Save and Post-Save to avoid errors.
Use Save Mode codes to control behavior for different save actions.
Test with “dirty” records (unsaved changes) to ensure scripts trigger correctly.
Disable and re-enable handlers during testing to prevent conflicts.

Wrapping Up

Mastering OnSave Power Apps ensures your applications are more dynamic, user-friendly, and aligned with business needs.

If you’re working on model-driven apps, mastering these techniques will save time, reduce complexity, and make your apps more responsive.

Want to deepen your expertise in Power BI and Dataverse? Explore Power BI Training with Data Bear.