web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Model Driven App, Java...
Power Apps
Suggested Answer

Model Driven App, Javascript : from JS client script fetch JS web ressource

(0) ShareShare
ReportReport
Posted on by 67
Hi, 
 
I'm writing some JS client scripts to act on model-driven app forms, and I see myself reusing the same code snippets all the time.
 
I would like to have a helper JS file with useful and recurring functions that would be saved as a web resource in my Dataverse solution.
From my scripts, how can I fetch and execute the helper web resource and have the helper code snippets added to namespaceMain? (Ideally, without having to add the helper web resource to each forms library)
 
Thanks!
 
 
Helper file: 
(function (global) {
    global.namespaceMain = global.namespaceMain || {};

    global.namespaceMain.helpers = {
        usefulFunction1: function (formContext, foo) {
            formContext.doThings1With(foo);
        },

        usefulFunction2: function (formContext, bar) {
            formContext.doThing2With(bar);
        },

        usefulFunction3: function (formContext, baz) {
            formContext.doThing3With(baz);
        }
    };

})(globalThis);
 
Typical script file: 
(function (global) {
    global.namespaceMain = global.namespaceMain || {};
    
    //How can I do this here: fetch web resource and run it

    global.namespaceMain.form1 = {
        onload: function (executionContext) {
            const formContext = executionContext.getFormContext();

            //If we need to pass executionContext to fetch web resource do it here (plan b)

            formContext.doThingsOnLoad();
        },

        onChangeField1: function (executionContext) {
            const formContext = executionContext.getFormContext();
            global.namespaceMain.helpers.usefulFunction1(formContext, foo)
            formContext.doThingsOnChangeField1();
        },

        onChangeField2: function (executionContext) {
            const formContext = executionContext.getFormContext();
            global.namespaceMain.helpers.usefulFunction2(formContext, bar)
            formContext.doThingsOnChangeField2();
        },

        onChangeField3: function (executionContext) {
            const formContext = executionContext.getFormContext();
            global.namespaceMain.helpers.usefulFunction3(formContext, baz)
            formContext.doThingsOnChangeField3();
        },
    };

})(globalThis);
 
I have the same question (0)
  • Suggested answer
    QuentinDURIEZ Profile Picture
    547 Moderator on at
    Hi,

    You can add a dependency to a webresource, with that, you helper script will be loaded when the script is loaded

    Go to your webresource > Edit in classic > Dependencies > add the helper script web resource

    Regards,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 765 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 272

Last 30 days Overall leaderboard