Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Beginner Error - Problem with javascript to learn

(0) ShareShare
ReportReport
Posted on by

I am starting in script development, I am performing some test for learning based on microsoft documentation. I am writing an opportunity script to:
- Display a message when a form is loaded
- Assign description text for when a field has its content changed
- Display a message when saved

I tried several ways to run the code, but always get an error message. Can anybody help me?

_________________________________________________________

// A namespace defined for the sample code
// As a best practice, you should always define 
// a unique namespace for your libraries
var Sdk = window.Sdk || {};
(function () {
    // Define some global variables
    var myUniqueId = "_myUniqueId"// Define an ID for the notification
    var currentUserName = Xrm.Utility.getGlobalContext().userSettings.userName// get current user name
    var message = currentUserName + ": Your JavaScript code in action!";

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

        // display the form level notification as an INFO
        formContext.ui.setFormNotification(message"INFO"myUniqueId);

        // Wait for 5 seconds before clearing the notification
        window.setTimeout(function () { formContext.ui.clearFormNotification(myUniqueId); }, 5000);
    }

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

        // Automatically set some field values if the account name contains "Contoso"
        var Name = formContext.getAttribute("name").getValue();
        if (Name.toLowerCase().search("contoso"!= -1) {
            formContext.getAttribute("description").setValue("">https://www.contoso.com");
        }
    }

    // Code to run in the form OnSave event 
    this.formOnSave = function () {
        // Display an alert dialog
        Xrm.Navigation.openAlertDialog({ text: "Record saved." });
    }
}).call(Sdk);
  • Fubar Profile Picture
    2,752 on at
    RE: Beginner Error - Problem with javascript to learn

    The code in the link in my previous post will work on Opportunity if you comment out the following 2 lines (the attributes do not exist for Opportunities, but the others in the JavaScript do)

              // formContext.getAttribute("websiteurl").setValue("www.contoso.com");

               // formContext.getAttribute("telephone1").setValue("425-555-0100");

    (also, after you make changes to the JavaScript or the Form make sure you Publish, and sometimes when testing your changes you also need to refresh the Browser window <ctrl><f5> to force the browser to reload cached JavaScript etc).

  • Community Member Profile Picture
    on at
    RE: Beginner Error - Problem with javascript to learn

    When I use template code on an account form it works. I'm having trouble when I adapt code to run on an opportunity form.

  • Suggested answer
    Fubar Profile Picture
    2,752 on at
    RE: Beginner Error - Problem with javascript to learn

    @Arun,  he is attempting to run sample code that is similar but not 100% the same as that provided by Microsoft docs.microsoft.com/.../walkthrough-write-your-first-client-script  (I don't particularly like the structure of it or the use of "window" - but it is what it is)

    @Edersc, I used the code as provided by MSFT in the link above, hooked up the onLoad and it works without any errors (I didn't hook up any other events) - your original code does not 100% match that from MSFT so not sure if you have copied it from an old version or someone else - anyway try with the code from link in this message.  (note, this was on the Account entity and the Account for Interactive Experience form, and accessed it from a '...…. Hub' app).

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    RE: Beginner Error - Problem with javascript to learn

    I believe you are not calling the right method name. refer this: stackoverflow.com/.../7920473

    Also as you are using ES6 standards to call using prototype, I'm not sure about it.

    developer.mozilla.org/.../call

  • Suggested answer
    Anas Rafik Profile Picture
    365 on at
    RE: Beginner Error - Problem with javascript to learn

    Try to fix this line of code

    //from

    var formContext = executionContext();

    //to

    var formContext = executionContext.getFormContext(); // get formContext

  • Community Member Profile Picture
    on at
    RE: Beginner Error - Problem with javascript to learn

    Thank you Arun for helping me!

    I made the changes but the messages are still displayed. Below are the messages:

    Error in custom event for this field.

    Field: window

    Event: onload

    Error: undefined

    Error in custom event for this field.

    Field: crmForm

    Event: onsave

    Error: undefined

    Error in custom event for this field.

    Field: name

    Event: onchange

    Error: undefined

    As you advised me, I already selected the field "Pass execution context with a first parameter" and updated the code. With the change follows the new code:

    // A namespace defined for the sample code

    // As a best practice, you should always define

    // a unique namespace for your libraries

    var Sdk = window.Sdk || {};

    (function () {

       // Define some global variables

       var myUniqueId = "_myUniqueId"; // Define an ID for the notification

       var currentUserName = Xrm.Utility.getGlobalContext().userSettings.userName; // get current user name

       var message = currentUserName + ": Your JavaScript code in action!";

       // Code to run in the form OnLoad event

       this.formOnLoad = function (executionContext) {

           var formContext = executionContext.getFormContext();

           // display the form level notification as an INFO

           formContext.ui.setFormNotification(message, "INFO", myUniqueId);

           // Wait for 5 seconds before clearing the notification

           window.setTimeout(function () { formContext.ui.clearFormNotification(myUniqueId); }, 5000);

       }

       // Code to run in the attribute OnChange event

       this.attributeOnChange = function (executionContext) {

           var formContext = executionContext.getFormContext();

           // Automatically set some field values if the account name contains "Contoso"

           var Name = formContext.getAttribute("name").getValue();

           if (Name.toLowerCase().search("contoso") != -1) {

               formContext.getAttribute("description").setValue("www.contoso.com");

           }

       }

       // Code to run in the form OnSave event

       this.formOnSave = function () {

           // Display an alert dialog

           Xrm.Navigation.openAlertDialog({ text: "Record saved." });

       }

    }).call(Sdk);

    Regards,

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    RE: Beginner Error - Problem with javascript to learn

    You haven't shared the error message to understand the issue.

    Couple of things to troubleshoot further:

    1. Make sure you are passing the parameter by checking the "Pass execution context as first parameter" like explained in the below post

    docs.microsoft.com/.../clientapi-execution-context

    2. Change the below line:

    //buggy code

    var formContext = executionContext();

    //fixed code

    var formContext = executionContext.getFormContext();

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 97

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans