How To Retrieve the ObjectTypeCode by Table Name in Dynamics 365

 In Dynamics 365 (Dataverse), each entity is assigned a unique numerical identifier known as the ObjectTypeCode. This code is used internally for various purposes, such as filtering system forms and working with metadata. While some approaches may attempt to query metadata directly via unsupported endpoints (like EntityDefinitions), the recommended and supported method is to use the Xrm.Utility.getEntityMetadata function.

In this article, we’ll show you how to retrieve the ObjectTypeCode for any entity using this method.

Using Xrm.Utility.getEntityMetadata

The following JavaScript function demonstrates how to retrieve the ObjectTypeCode given the logical name of the entity. It checks if the function is available and then calls it to get the required metadata.

/**
 * Retrieves the object type code for a given entity logical name using Xrm.Utility.getEntityMetadata.
 *
 * @param {string} entityLogicalName - The logical name of the entity.
 * @param {function} successCallback - Callback function receiving the object type code.
 * @param {function} errorCallback - Callback function receiving error details.
 * @returns {void}
 *
 * Usage:
 * GetEntityObjectTypeCode("account", function(code) {
 *     console.log("ObjectTypeCode:", code);
 * }, function(error) {
 *     console.error("Error:", error);
 * });
 */
function GetEntityObjectTypeCode(entityLogicalName, successCallback, errorCallback)
{
    if (Xrm.Utility && typeof Xrm.Utility.getEntityMetadata === "function")
    {
        Xrm.Utility.getEntityMetadata(entityLogicalName, ["ObjectTypeCode"]).then(
        function (metadata)
        {
            successCallback(metadata.ObjectTypeCode);
        },
        function (error)
        {
            errorCallback(error);
        });
    }
    else
    {
        errorCallback("Xrm.Utility.getEntityMetadata is not available.");
    }
}

How It Works

  1. Function Check:
    The function first verifies that Xrm.Utility.getEntityMetadata is available. This method is supported in Dynamics 365 Online (v9+) environments.

  2. Retrieving Metadata:
    If available, it retrieves the metadata for the specified entity, selecting only the ObjectTypeCode attribute to keep the response lightweight.

  3. Callbacks:

    • Success Callback: Returns the retrieved ObjectTypeCode.
    • Error Callback: Provides details if the retrieval fails or if the function isn’t available.

Example Usage

Below is an example of how to call the GetEntityObjectTypeCode function. This snippet retrieves the ObjectTypeCode for the "account" entity and logs it to the console.

GetEntityObjectTypeCode("account",
    function(code)
    {
        console.log("The ObjectTypeCode for account is:", code);
    },
    function(error)
    {
        console.error("Error retrieving ObjectTypeCode:", error);
    });

Conclusion

Using Xrm.Utility.getEntityMetadata is a modern and supported way to retrieve metadata, including the ObjectTypeCode, in Dynamics 365. This approach ensures compatibility and leverages the built-in Web API capabilities of the platform, avoiding potential issues with deprecated or unsupported endpoints.

Feel free to share this article with your colleagues or community members to help them work with entity metadata in Dynamics 365 more effectively.

No comments:

Post a Comment