Friday, July 1, 2011

How to get the Entity Type Code / Object Type Code in CRM 2011 - Part 2

This is a followup to my previous post about How to get the Entity Type Code in CRM 2011. While that method works, it requires you to go to a page and manually get the type code for the entity. What we really need is a way through script to get the entity type code / object type code by passing an entity name. Well, the code below uses Microsoft's own logic and will return the entity type code / object type code for the entity name you provide it.  Let me know if you have any trouble or have any suggestions on how to improve this.

var getObjectTypeCode = function(entityName) {
/// <summary>
/// Gets the EntityTypeCode / ObjectTypeCode of a entity
/// </summary>
/// <param name="entityName" type="string">
/// Name of entity to return object type code of
/// </param>
/// <returns type="int" /> 
 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;
 }
}

No comments:

Post a Comment