Dynamics 365 CRM – How to Retrieve Entity Information in JavaScript
When developing customizations in Microsoft Dynamics 365 CRM, one of the key aspects is being able to retrieve information about the current record on a form. This is essential for implementing custom logic, making API calls, or integrating with external systems.
In this article, you’ll learn the most important properties you can use in JavaScript within Dynamics 365 CRM, along with code examples you can apply directly in your projects.
Most Commonly Used Properties in Dynamics 365 CRM JavaScript Forms
1. EntityId
Returns the unique identifier (GUID) of the currently open record. This is critical for API calls or any record-specific operations.
var entityId = Xrm.Page.data.entity.getId();
console.log("EntityId:", entityId);
2. EntityIdWithoutBraces
Retrieves the record’s GUID without curly braces { }, which are often not accepted in service calls or integrations.
var entityId = Xrm.Page.data.entity.getId().replace(/[{}]/g, "");
console.log("EntityIdWithoutBraces:", entityId);
3. EntityName
Returns the logical name of the entity (e.g., account, contact, opportunity). This is particularly useful for dynamically identifying which entity the script is running on.
var entityName = Xrm.Page.data.entity.getEntityName();
console.log("EntityName:", entityName);
4. EntitySetName
Retrieves the OData entity set name (e.g., accounts, contacts). This is necessary when working with the Web API or OData queries.
var entitySetName = Xrm.Page.context.getQueryStringParameters()["etn"];
console.log("EntitySetName:", entitySetName);
5. IsDirty
Indicates whether the form has unsaved changes. It returns true if there are pending modifications and false otherwise. This helps prevent users from closing the form without saving.
var isDirty = Xrm.Page.data.entity.getIsDirty();
console.log("IsDirty:", isDirty);
6. AttrName (Primary Attribute)
Retrieves the value of the entity’s primary attribute, such as Name in account or contact. This is commonly used for display purposes or to add validation logic.
var attrName = Xrm.Page.getAttribute("name").getValue();
console.log("Primary Attribute (Name):", attrName);
Best Practices When Working with Properties in D365 CRM
- Always use the modern API (
formContext) instead ofXrm.Page, as the latter is deprecated. - Validate if a value is
nullorundefinedbefore applying your logic. - Use
EntitySetNameto properly construct your Web API queries. - Take advantage of
IsDirtyto improve user experience and prevent data loss.
Conclusion
Mastering these properties will allow you to build more robust and efficient scripts in Dynamics 365 CRM. Whether you need to identify a record, validate unsaved changes, or interact with the Web API, these functions are the foundation of any advanced customization.
By implementing them correctly, you’ll improve user experience and make integrations with other systems much easier.
Comments
Post a Comment