CRM 2011/2013 – Javascript to get the object type code of an entity


I had some javascript code, which created a new entity and then opened a new browser with the url of the newly created entity.

I did this by getting the standard url for that record (using the copy a link button) and then dynamically adding in the guid of the new record.

but when I moved the code from DEV to TEST, the code created the new record but then when it came to displaying the record it was erroring and throwing an error saying

record is unavailable

Now I don’t like errors, I find them annoying and this one was annoying me, my fantastic code brought crashing down by a move of servers.  I dimly remembered something like this before but I dimly couldn’t remember what it was.

I compared the url from the newly created record and the url I was trying to open they were the same, until I finally noticed one small number was different.



yes, the number after etc was 2 different.  So what is the ETC number

The ETC is Object Type Code of an entity and every entity has it’s own unique value.  In the link below you can see all the values for the default entities in CRM

Your new custom entities I think will start counting up from the number 10000 but BEWARE, these numbers could be different between servers because the entities could have been created in a slightly different order.  I was speaking to a CRM buddy and he said in CRM 4 they had no method to get the Object Type Code and they had to keep a config value with the Object Type Code in, which enabled them to modify between each server.

So I need to amend my Javascript to include this number

In CRM 2011/CRM 2013 there is a very easy way to do this

var ObjectTypeCode= Xrm.Page.context.getQueryStringParameters().etc

I found we had a common Javascript function which returned the value, you pass the entity name into JavaScript and it will return you the number

var entityNumber = GetObjectTypeCode(“hosk_entity”);

<code>function GetObjectTypeCode(entityName) {
try {
var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
lookupService.SetParameter("entityName", entityName);
var result = lookupService.Execute();

if (result.Success && typeof result.ReturnValue == "number") {
return result.ReturnValue;
else {
return null;
catch (ex) {
throw ex;

Brian Bayes told me about a much easier way to open a new record as long as you have the guid




6 thoughts on “CRM 2011/2013 – Javascript to get the object type code of an entity

  1. Marc March 10, 2014 / 1:20 pm

    If all you want to do is open an entity record you can use etn=entityschemaname instead.


  2. michaeldmayo May 12, 2014 / 5:35 pm

    Yea Marc, but there’s a good reason to need this aside from opening forms – like the layoutXml of the addCustomView… great post Ben!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s