DEV Community

Cover image for Force refresh of custom scripts in Power Platform model-driven apps
Kinga
Kinga

Posted on

1

Force refresh of custom scripts in Power Platform model-driven apps

How do you force custom script refresh in your Model Driven apps?

I’m sure it’s sometimes OK to wait until the problem will solve itself, but if you delete a column that the script is referencing, the user will see an error.
That’s not a great experience.

Me, I’m using the Form.OnLoad event to compare the version defined in the form with a version declared in the JavaScript.

For example, the following file is added as a WebResource to my solution:

var ECSLibrary = window.ECSLibrary || {};
(function () {

    const currentVersion = "0.1.1.3";

    this.OnFormOpen = function (executionContext, versionNumber) {

        const formContext = executionContext.getFormContext();

        if (currentVersion !== versionNumber) {
            formContext.ui.setFormNotification("The form is outdated. Please refresh the page using Ctrl+F5 buttons.", "WARNING");
        }
    }
}).call(ECSLibrary);
Enter fullscreen mode Exit fullscreen mode

And associated with the form’s OnLoad using the current configuration:

Image description

Every time I’m making changes, I increase the version number in both places. If the user has old, cached script version, the numbers will differ and a warning is displayed on top of the page.

And if you are using Dataverse DevTools, and writing code in typescript it's very similar:

const currentVersion = "0.1.1.3";

export const OnLoad = (executionContext: Xrm.Events.EventContext, versionNumber:string): void =>
    const formContext: Xrm.Idapps_externalcloudservice = executionContext.getFormContext();

    if (currentVersion !== versionNumber) {
        formContext.ui.setFormNotification("The form is outdated. Please refresh the page using Ctrl+F5 buttons.", "WARNING","_formVerWarning");

    }
};
Enter fullscreen mode Exit fullscreen mode

Image description

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay