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

get-foto-armin-kc3bcbelbeck-wikimedia-commons

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.

main.aspx?etc=10010&extraqs=formid

main.aspx?etc=10012&extraqs=formid

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

http://technet.microsoft.com/en-us/library/ms955729.aspx

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;
}
}
</code>

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.

    Like

  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!

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.