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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Loading JavaScript on opening the app

(3) ShareShare
ReportReport
Posted on by 16
Hi,
I want to have a javascript executed as the app is opened by the user. Ideally, this code should be executed pretty much no matter where the user is in the system.
Is there a way to do it or are there any workarounds to do something similar?
 
Thanks,
Jimmy Owen
I have the same question (0)
  • Verified answer
    Holly Huffman Profile Picture
    6,522 Super User 2025 Season 2 on at
    Loading JavaScript on opening the app
    Hi there! Good morning, evening, or afternoon - depending on where you are :) Hope you are well today! 
     
    To execute JavaScript when the app is opened, regardless of where the user is in the system, you can use JavaScript web resources in Dynamics 365. However, there isn't a direct way to run JavaScript globally across the entire app on load. Instead, you can achieve similar functionality with the following workarounds:
     
    1. Form Scripts: Attach your JavaScript to the OnLoad event of specific forms. This ensures the script runs whenever a user opens a particular form. However, this approach is limited to specific entities.
    2. Ribbon Workbench: Use the Ribbon Workbench to add custom JavaScript to buttons or commands. This can trigger your script when users interact with specific UI elements.
    3. Custom Web Resources: Create a custom HTML web resource that includes your JavaScript. You can embed this web resource in dashboards or forms that are frequently accessed by users.
    4. Custom Page or App: If you need a truly global solution, consider creating a custom page or app that acts as the entry point for users. You can include your JavaScript in this custom page, ensuring it runs whenever the app is accessed.
    5. Business Rules or Plugins: While not JavaScript, you can use business rules or plugins to achieve certain automation or logic across the system.
     
    Hope this helps some!
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,833 Super User 2025 Season 2 on at
    Loading JavaScript on opening the app
    Hello Jimmy,
     
    You're looking to execute JavaScript code globally when a user opens the Dynamics 365 app, regardless of their location within the system. While there's no single "app-level OnLoad" event, you can achieve this using a combination of techniques and workarounds:
     
    Understanding the Challenge:
    • No App-Level OnLoad: Dynamics 365 doesn't have a direct event that fires when the entire app is loaded.
    • Form-Based Events: JavaScript execution is typically tied to form events (OnLoad, OnSave, etc.).
    • Global Execution: You need to find a way to execute your code across all forms or a significant portion of the app.
     
    Workarounds and Techniques:
    1. Form OnLoad on Common Forms:
      • Identify Common Forms: Determine the most frequently used forms in your app (e.g., dashboard, account, contact, opportunity).
      • Add JavaScript to OnLoad: Add your JavaScript web resource to the OnLoad event of these common forms.
      • Pros: Relatively straightforward, covers a large portion of user activity.
      • Cons: Doesn't guarantee execution on every form, requires maintenance if new forms are added.
    2. Dashboard OnLoad (Good Starting Point):
      • Dashboard as Entry Point: If users frequently start with a specific dashboard, add your JavaScript to its OnLoad event.
      • Pros: Executes early in the user's session.
      • Cons: Doesn't cover scenarios where users navigate directly to other forms.
    3. SiteMap Modification (Advanced):
      • Add JavaScript to SiteMap: You can modify the SiteMap XML to include JavaScript execution when certain areas of the app are loaded.
      • Pros: Potentially more global execution.
      • Cons: Complex, requires modifying the SiteMap, which can be risky and impact performance.
      • Not officially supported: Microsoft does not recommend this approach.
    4. PCF Control (Power Apps Component Framework):
      • Create a PCF Control: Develop a lightweight PCF control that contains your JavaScript code.
      • Add to Common Forms: Add this PCF control to the common forms you identified earlier.
      • Pros: More structured approach, can be reused across forms.
      • Cons: Requires PCF development skills.
    5. Web API Calls from Browser Console (For Testing/Debugging):
      • Browser Console: Use the browser's developer console to execute JavaScript code directly.
      • Dynamics 365 Web API: Use the Dynamics 365 Web API to interact with data.
      • Pros: Useful for testing and debugging.
      • Cons: Not a production-ready solution, requires manual execution.
     
    Example JavaScript Code (Form OnLoad):
    JavaScript
     
    function appStartup() {
        // Your global JavaScript code here
        console.log("App startup script executed.");

    // Example: Make a Web API call
        Xrm.WebApi.retrieveMultipleRecords("account").then(
            function success(result) {
                console.log(result);
            },
            function (error) {
                console.log(error.message);
            }
        );
    }

     
    Implementation Steps:
    1. Create a JavaScript Web Resource:
      • Go to Settings > Customizations > Customize the System.
      • Navigate to Web Resources.
      • Create a new JavaScript web resource.
    2. Add JavaScript to Common Forms:
      • Go to Settings > Customizations > Customize the System.
      • Navigate to Entities and select the common forms.
      • Open the form and go to Form Properties.
      • Add the JavaScript web resource to the OnLoad event.
    3. Publish Customizations:
      • Publish your customizations.
     
    Key Recommendations:
    • Form OnLoad on Common Forms: This is generally the most practical and supported approach.
    • PCF Control (If Applicable): If you have PCF development skills, this provides a more structured solution.
    • Test Thoroughly: Test your code in a non-production environment.
    • Performance Considerations: Be mindful of the performance impact of your global JavaScript code.
    By using these workarounds, you can achieve a close approximation of global JavaScript execution in Dynamics 365.
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak
  • Vahid Ghafarpour Profile Picture
    11,980 Super User 2025 Season 2 on at
    Loading JavaScript on opening the app
    If any of the responses helped resolve your issue, please take a moment to mark the best answer. This helps others in the community quickly find solutions to similar problems.

    To do this, simply click the "Does this answer your question?" button on the most helpful response and like the helpful posts. If your issue is still unresolved, feel free to provide more details so the community can assist further!

    Thanks for being an active part of the Dynamics 365 Community! 😊

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 149

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 132

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 123 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans