Skip to main content

Notifications

Step by Step to Create a Client JavaScript in Dynamics 365 CRM

Objective

After completing this walkthrough below, you will know how to use JavaScript code in Dynamics 365 CRM.

Requirement

If the Industry field in the Account entity is changed, an alert message pops up that says ”Selected Industry is ......

1777.pastedimage1682324063710v1.png

Step 1: Write your JavaScript code

"use strict";
if (typeof (TestNameSpace) === "undefined" || TestNameSpace === null) {
    var TestNameSpace = {};
}

if (typeof (TestNameSpace.Account) === "undefined") {
    TestNameSpace.Account = {};
}

TestNameSpace.Account = {
     
    industryOnChange: function (executionContext) {
        let formContext = executionContext.getFormContext();
        
		var selecteIndustry = formContext.getAttribute("industrycode").getText();
		
		var alertStrings = { confirmButtonLabel: "OK", title: "Selected Industry" };
        var alertOptions = { height: 300, width: 400 };
        alertStrings.text = "Selected Industry is "   selecteIndustry ;

        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
}

Detailed code explanation

Define namespace: The code starts by defining a namespace for your custom script. As a best practice, you should always create namespaced JavaScript libraries to avoid having your functions overridden by functions in another library.

8228.pastedimage1682326346200v2.png

Function to execute on the [On Change] event:

The [TestNameSpace.Account.industryOnChange] Function will be associated with the Industry column in the account form so that it gets executed only when you change the account name value.

6403.pastedimage1682326523431v3.png

Step 2: Upload your code as a Web Resource

Now your code is ready, you need to upload it into your solution.

1, Locate your custom Solution and create a new Web Resource

8304.pastedimage1682326770872v4.png

2, Fill in the fields and click the Text Editor.

6102.pastedimage1682327015731v5.png

3, Copy->Paste the source code

8103.pastedimage1682327117946v6.png

4, Publish

pastedimage1682340605902v1.png

Step 3:  Navigate to the Form.  Entity -> Forms

8267.pastedimage1682327813447v9.png

Step 4: Click Form Properties

3225.pastedimage1682327931199v10.png

Step 5:  Add web Resource to the Form Libraries

0488.pastedimage1682328056400v11.png

2480.pastedimage1682328146522v12.png

Step 6: Select the Field and Event

4382.pastedimage1682328313413v14.png

Step 7:  Bind the Function Name

2021.pastedimage1682328418404v15.png

Step 8: Publish the form

8540.pastedimage1682328493408v16.png

Test it

Run as expected

6457.pastedimage1682329740410v17.png

The End

Comments

*This post is locked for comments