Skip to content
/

Get the Entity Type Code using JavaScript only (not using remote services)

Every Microsoft Dynamics CRM developer has faced this problem: How to get the entity type code for your custom entity in JavaScript. If you need the entity type code for the current form there are several supported ways to get it. But when you want the entity type code for a different custom entity, you are in trouble. The SDK has no real solution for this, and hard-coding is no option as the numbers can change per deployment.

So how do most people solve this? A popular choice is the unsupported Remote Command and Lookup service. Because of the asynchronous nature the use of a service adds a performance penalty and complexity to your code. It also seems this will break in CRM 2015 Update 1.

Digging through the client side object model I found a nice, although still unsupported, treasure which works with CRM 2011 and up, including the latest installment: 2015 update 1.

Mscrm.EntityPropUtil.EntityTypeName2CodeMap

This class contains a dictionary of all out-of-the-box entities but also all custom entities. And as it's already loaded in memory there is no performance loss.

Form JavaScript

When using JavaScript in a form the goodies are readily available:

Mscrm.EntityPropUtil.EntityTypeName2CodeMap.account
// 1

Mscrm.EntityPropUtil.EntityTypeName2CodeMap["account"]
// 1

Mscrm.EntityPropUtil.EntityTypeName2CodeMap.wv_country
// 10025

Web Resource

From a web resource just target the parent window:

window.parent.Mscrm.EntityPropUtil.EntityTypeName2CodeMap

Or if you have included the ClientGlobalContext.js.aspx, you can also use the class directly:

Mscrm.EntityPropUtil.EntityTypeName2CodeMap

Turbo Forms

With the introduction of the turbo forms in CRM 2015 update 1 you need an additional line of code to make the mapping work:

if (Mscrm.EntityPropUtil == null) {
    Mscrm.EntityPropUtil = parent.Mscrm.EntityPropUtil;
}

From entity type code to entity type name

If you want to use this map to get the entity type name from the entity type code you can use the following line of JavaScript:

Object.keys(Mscrm.EntityPropUtil.EntityTypeName2CodeMap).filter(function(key) {
  return Mscrm.EntityPropUtil.EntityTypeName2CodeMap[key] === 10025
})[0]
// "wv_country"

It's that easy!

7 Comments

  1. /

    Thank you so much to share this information.
    Helps me alot.

  2. /

    Thank You ! That's the coolest way to get ETC and ETN.

  3. /

    Thanks a lot!!.

    it worked me... Saved my time!! :-)

  4. /

    I tried this , But not working, using CRM 2016 on promise version.
    var entityCode= Mscrm.EntityPropUtil.EntityTypeName2CodeMap["new_customentity"];
    alert(entityCode);
    anything Im missing ?

    • /

      I'm not sure, works for me with CRM 2016 on-premise

    • /

      If it doesn't work please try the below one which works for me...
      parent.Mscrm.EntityPropUtil.EntityTypeName2CodeMap["EntityName"];
      else
      window.parent.Mscrm.EntityPropUtil.EntityTypeName2CodeMap["EntityName"];
      Hope it works..!

Trackbacks and Pingbacks

  1. Hosk’s Top CRM Articles of the week – 19th November – Hosk's Dynamic CRM Blog

Leave a comment