Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Dynamics 365, JavaScript not working on the onLoad event

Posted on by 460

Hi,

I have the following JavaScript:

var Sdk = window.Sdk || {};
(function () {
       // Define global variable
       var currentUserName = Xrm.Utility.getGlobalContext().userSettings.userName;

     // Code to run in the form OnLoad event
    this.formOnLoad = function (executionContext) {
        var formContext = executionContext.getFormContext();
    }

    // Code to run in the attribute OnChange event
    this.attributeOnChange = function (executionContext) {
        var formContext = executionContext.getFormContext();

        // Automatically set Current User
          alert(currentUserName);  
                        formContext.getAttribute("new_currentuser").setValue(currentUserName);  
}
}).call(Sdk);

However the Field: new_currentuser is not getting populated and the alert is not displaying.

Form Properties:  I have the New Web Resource
Form OnLoad:     Sdk.formOnLoad
Field Property:     OnChange event:  Sdk.attributeOnChange

All are enabled, saved and publish

What am I missing.

Thanks for your help

*This post is locked for comments

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Dynamics 365, JavaScript not working on the onLoad event

    Hi,

    I will suggest you to use debugger keyword and debug your js to see where it's failing, you will learn how to troubleshoot js code and you can fix your code as well.

    docs.microsoft.com/.../debug-javascript-code

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans