Get Form Information by Table Logical Name by JavaScript in Dynamics 365

This article introduces a JavaScript function designed to retrieve and log details about system forms associated with a specific entity within Dynamics 365. The function utilizes the Dynamics 365 Web API, demonstrating how to make API calls and process the data efficiently.

Function Overview

The function, getFormsForEntity, takes an entity's logical name as its input and fetches all related forms. It logs details such as the form name, ID, and description, providing a straightforward way to access and review form metadata directly from the console. This can be particularly useful for developers working on customizations or integrations that depend on specific form configurations.

Code Explanation

Here's the function in its entirety:

async function getFormsForEntity(entityLogicalName) {
    // Constructing the OData query to fetch form metadata
    const query = `/api/data/v9.0/systemforms?$select=name,formid,description&$filter=objecttypecode eq '${entityLogicalName}' and type eq 2`; // Note: 'type eq 2' might need adjustment based on your form types

    try {
        const response = await fetch(Xrm.Utility.getGlobalContext().getClientUrl() + query, {
            method: 'GET',
            headers: {
                'OData-MaxVersion': '4.0',
                'OData-Version': '4.0',
                'Accept': 'application/json',
                'Content-Type': 'application/json; charset=utf-8',
                'Prefer': 'odata.include-annotations="*"'
            }
        });

        const data = await response.json();
        if (response.ok) {
            console.log('Forms for Entity', entityLogicalName, ':');
            data.value.forEach(form => {
                console.log(`Name: ${form.name}, ID: ${form.formid}, Description: ${form.description || 'No description'}`);
            });
            return data.value; // This returns the list of forms, could be used further in the code
        } else {
            console.error('Error retrieving forms:', data.error);
            return null;
        }
    } catch (error) {
        console.error('Exception when fetching forms:', error);
        return null;
    }
}


Test sample(from web browser console):




How It Works

  • The function constructs a Web API query to retrieve the name, formid, and description of all forms associated with the specified entity.
  • It uses the fetch method to make the API call, handling both successful responses and errors gracefully.
  • Upon a successful API call, it logs each form's details to the console, providing immediate visibility into the form configurations.

Use Cases

  • Customization and Configuration: Developers can use this function to identify the forms available for a particular entity, aiding in customization tasks.
  • Integration Development: When integrating external systems with Dynamics 365, knowing form IDs and details can help map data and functionality accurately.
  • Audit and Compliance: For auditing purposes, quickly retrieving and reviewing form metadata can help ensure compliance with organizational standards and practices.

Conclusion

Leveraging the Dynamics 365 Web API to fetch system form metadata programmatically can greatly enhance the capability to manage and customize your CRM environment effectively. By utilizing JavaScript within the Dynamics context, developers can create flexible, powerful solutions tailored to their specific needs.


No comments:

Post a Comment