Code notes

One rule, three languages: Power Fx, C# and JavaScript

Three small customer-name validation examples, with readable code and downloadable source.

One small rule. Three places to put it. A customer name should contain more than spaces—but where should that check happen?

These short examples show how Power Fx, C#, and JavaScript can support the same business intent. They are independent teaching samples, with different integration points.

01 / Power Fx: guide the person entering data

Use this formula in a save button’s OnSelect property. It assumes a classic text input named txtName, bound to the name field in frmCustomer. The form must already have its data source and required fields configured. This example uses English-locale formula separators.

Power FxDownload source ↓
// Save button OnSelect — classic text input
If(
    IsBlank(Trim(txtName.Text)),
    Notify("Enter a customer name.", NotificationType.Error),
    SubmitForm(frmCustomer)
)

The check catches blank or space-only input before submitting. Put success handling in the form’s OnSuccess and submission error handling in OnFailure. The input check does not guarantee the save will succeed.

02 / C#: keep a reusable rule near the data

This standalone helper rejects blank input and returns a trimmed name. It can be called from a server-side operation. It does not register itself as a Dataverse plug-in or enforce a database rule on its own.

C#Download source ↓
using System;

// A reusable helper, not a complete Dataverse plug-in.
public static class CustomerName
{
    public static string Normalize(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new ArgumentException(
                "Enter a customer name.", nameof(value));
        }

        return value.Trim();
    }
}

For a Dataverse implementation, call the rule from an appropriately registered synchronous plug-in, handle partial updates, and translate validation failures into a clear InvalidPluginExecutionException. That integration is separate from this helper. Test null, empty, whitespace-only, and valid names before connecting it to a write operation.

03 / JavaScript: give useful feedback on a CRM form

Add this script as a JavaScript web resource to an Account main form containing the name column. Register checkCustomerName on form OnLoad and name OnChange, passing the execution context to each handler.

JavaScriptDownload source ↓
// Register on Account form OnLoad and name OnChange.
// Enable “Pass execution context as first parameter”.
function checkCustomerName(executionContext) {
    const form = executionContext.getFormContext();
    const name = form.getAttribute("name");
    if (!name) return;

    const notificationId = "tunex-name-check";
    const value = name.getValue() || "";
    if (!value.trim()) {
        form.ui.setFormNotification(
            "Enter a customer name.", "WARNING", notificationId);
    } else {
        form.ui.clearFormNotification(notificationId);
    }
}

The warning clears when the name contains text. A form-level warning is informational: it does not block saving, validate imports, or protect API writes. Use column requirements and server-side validation for enforcement.

Keep the rule understandable

Client checks help people correct mistakes quickly. A server-side rule provides a consistent boundary when that rule must apply to every write path. Keep the expected behavior documented so the implementations do not drift apart.

Source files, ready to reuse

Each example has a plain-text source download. You can select and copy the code directly, too. These examples are maintained with the article and do not execute on this page.

References: Power Fx blank checks and model-driven form notifications.

How does this apply to your business?

Discuss one workflow or application and a practical next step.

Talk with Kostya