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 ......”
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.
Function to execute on the [On Change] event:
The [TestNameSpace.Account.industryOnChange] F
unction will be associated with the Industry column in the account form so that it gets executed only when you change the account name value.
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
2, Fill in the fields and click the Text Editor.
3, Copy->Paste the source code
4, Publish
Step 3: Navigate to the Form. Entity -> Forms
Step 4: Click Form Properties
Step 5: Add web Resource to the Form Libraries
Step 6: Select the Field and Event
Step 7: Bind the Function Name
Step 8: Publish the form
Test it
Run as expected
The End
*This post is locked for comments