Part 15.4 - Understanding Client API and Form Context in JavaScript Dynamics 365 CRM


1. Understanding Client API Objects:

==> Client API is a collection of JavaScript libraries that can be used to perform operations on forms, fields, and entities. It is commonly used for:

  • Validating data
  • Automating tasks
  • Enhancing user experience
  • Implementing custom business logic

Benefits of Using Client API

  • Real-time validation
  • Automate repetitive tasks
  • Control field visibility and accessibility
  • Perform actions based on form events



Article content


Overview of Form Context:

==> In Dynamics 365 CRM, Form Context is a crucial concept when working with JavaScript.

==> It allows developers to interact with form data, controls, and UI elements dynamically.

==> Understanding Form Context helps you customize forms, perform data manipulation, and execute custom business logic using JavaScript.


2. What is Form Context:

==> Form Context refers to the object that provides access to the current form and its data in Dynamics 365 CRM.

==> It allows JavaScript to read and manipulate the form fields, sections, tabs, and other UI components.

==> The Form Context object is the entry point to interact with fields, controls, and form data using Client API.

==> You can access the form context using the executionContext parameter in your function:


Why Use Form Context:

  • To access and update form data (e.g., read or write field values).
  • To show or hide fields and sections dynamically.
  • To validate data before form submission.
  • To display notifications and alerts to users.


3. What is Execution Context:

  • Execution Context is an object that contains information about the event that triggered a JavaScript function (e.g., OnLoad, OnSave, OnChange).
  • It acts as a gateway to access the Form Context.

Syntax:

Article content

  • executionContext.getFormContext() → Provides access to the current form instance.


4. Accessing Form Context in JavaScript:

==> We can access various form elements using Form Context.

Step 1: Accessing Field Values

  • Fields in CRM are referred to as Attributes.
  • You can get or set field values using JavaScript.


Get and Set Field Value:

Article content

  • .getValue() → Gets the value from the field.
  • .setValue() → Sets a new value for the field.


Step 2: Accessing Form Controls

==> Controls are the visual representation of fields on the form.

==> You can hide, disable, or enable controls using JavaScript.


Article content

Step 3: Accessing Sections and Tabs

  • Sections are logical groupings of fields.
  • Tabs are containers that group multiple sections.

Article content


5. Form Events in Dynamics 365 CRM

==> You can register JavaScript functions to execute on different form events using the Form Context.


Event Types:

  1. OnLoad - Triggered when the form loads.
  2. OnSave - Triggered when the form is saved.
  3. OnChange - Triggered when a field value changes.
  4. TabStateChange - Triggered when a tab is expanded or collapsed.


Example 1: OnLoad Event:

This event is used to perform actions when a form is opened.

Article content

Explanation:

  • Xrm.Utility.alertDialog() → Displays an alert with a custom message.


Example 2: OnChange Event

  • This event is triggered when a field value is changed by the user.
  • You can apply conditional logic to update other fields.

Article content

Explanation:

  • If the city is New York, the State field is automatically updated to NY.


Example 3: OnSave Event

==> You can validate data before the form is saved using the OnSave event.


Article content

Explanation:

  • Prevents form submission if the Account Name field is empty using preventDefault().


6. Working with Notifications Using Form Context:

==> You can display notifications using JavaScript to provide feedback to users.

Types of Notifications:

  1. Form Level Notification - Display messages at the top of the form.
  2. Field Level Notification - Show error messages on specific fields.
  3. Inline Notification - Provide in-line alerts below the field.


Example 1: Form Level Notification

Article content

Explanation:

  • Displays an INFO level notification on the form using setFormNotification().


Example 2: Field Level Notification:

Article content

Explanation:

  • Displays a notification on the Account Name field using setNotification().


Best Practices for Using Form Context

  • Always Use Execution Context: Ensure the correct form is accessed using executionContext.getFormContext().
  • Handle Errors Gracefully: Implement error handling using try-catch blocks.
  • Avoid Hardcoding: Use logical field names instead of hardcoding field values.
  • Minimize Form Events: Only use necessary events to avoid performance issues.

To view or add a comment, sign in

More articles by Vamshi Krishna Chadalavada

  • Part 3 - Microsoft Power Platform Environment

    ===> Before we start building Power Pages, we must understand Power Platform Environment. 1.

    1 Comment
  • Part 2 - Power Pages Architecture

    ===> When we say Power Pages Architecture, we are talking about: How a Power Pages website works behind the scenes when…

  • Part 1: Introduction to Power Pages

    First lets understand what problem does Power Pages Solve ? Lets take real time example: Imagine you work in a company…

  • Script Finder in XrmToolBox:

    Introduction ===> When working with complex Dynamics 365 CRM implementations, JavaScript customizations are widespread…

  • Microsoft Power Platform Solution Deployment Pipeline

    Introduction Managing Application lifecycle in the Microsoft Power Platform has always been critical to maintaining…

  • Understanding & Implementing Custom API in Dynamics 365 CRM

    What is a Custom API? ===> A Custom API in Dynamics 365 CRM is a way to define your own server-side business logic as a…

    12 Comments
  • Implementing Custom Actions in Dynamics 365 CRM

    Scenario: Loan Application Approval Process (Banking CRM) Business Requirement: When a loan application is submitted…

    5 Comments
  • Actions in Dynamics 365 CRM

    1. What is Action: In Dynamics 365 CRM, an Action is a custom business operation that you define once and can run…

    2 Comments
  • Part 2 - Microsoft 2025 Release Wave1

    ServiceNow → Dataverse Integration using Power Automate Objective: ===> We want to create a Power Automate flow that:…

  • Part 1 - Microsoft 2025 Wave Release1 - Sales Module

    ===> Microsoft added Smart AI tools (called agents) that help your sales team: Understand leads better. Pick the best…

Others also viewed

Explore content categories