Friday, September 30, 2011

Object Type Code in CRM 2011

Below, is the sample to get the Object Type code from the CRM 2011. Since, the object type code can be easily found from the entity record, which, may not be same in the other environment when you import the solution. We can fetch the Object Type code dynamically from the sample as in the below code.

var getObjectTypeCode = function(entityName) {
var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
lookupService.SetParameter("entityName", entityName);
var result = lookupService.Execute();

if (result.Success && typeof result.ReturnValue == "number") {
alert(result.ReturnValue);
return result.ReturnValue;
} 
else {
return null;
}
}

You may not find the "RemoteCommand" in the SDK, But, internally this has been in use in CRM 2011. More information to fallow...

3 comments:

Anonymous said...

If you decide to not hate yourself you can just use crmForm.ObjectTypeCode and save the waste of a call.

Anonymous said...

The GetObjectTypeCode(entityName) method mentioned above will not work in a HTML web resource because a HTML web resource is always contained within an IFrame & calling the CRM methods becomes a challenge.

Chaitanya... said...

I am totally with the second comment, but, the alternative approach in that case would be using Metadate service.