Code to get the object Type Code in 2015 update 1

Question Status

Verified
Jishad Shajahan asked a question on 11 May 2015 4:42 AM

I am using the following code to get the object type in ms dynamics CRM 2013 and 2015, in CRM 2015 Update 1 the code is not working?  

function GetObjectTypeCode(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;
}
}

Reply
Suggested Answer
Rajkumar Rajaraman responded on 11 May 2015 4:55 AM

Hi Jishad,

Have u tried this link?

crmbusiness.wordpress.com/.../crm-20112013-javascript-to-get-the-object-type-code-of-an-entity

Alsp post your error screenshot

Regards,

R.Rajkumar

"Please mark my answer as verified if you found it helpful"

Reply
Verified Answer
HIMBAP responded on 11 May 2015 5:08 AM

This is not a supported method, better to shift to supported methods. You can write a metadata query.

Check this: https://xrmservicetoolkit.codeplex.com/

Thanks

Reply
Jishad Shajahan responded on 11 May 2015 5:57 AM

Hi Rajkumar,

I tried it earlier, but it is not working. There is no error is showing, i put a debugger and see the code, once it is getting into the following line

var lookupService = new RemoteCommand("LookupService","RetrieveTypeCode");

it is return.

Reply
Suggested Answer
Rajkumar Rajaraman responded on 11 May 2015 6:08 AM

Hi Jishad,

Refer this thread for similar type of issue:

community.dynamics.com/.../67401

Hope this helps.

Regards,

R.Rajkumar

"Please mark my answer as verified if you found it helpful"

Reply
Jishad Shajahan responded on 11 May 2015 6:08 AM

Hi HIMBAP,

I gone through the link and downloaded the XrmServiceToolkit, what shall i do next?

Reply
Verified Answer
HIMBAP responded on 11 May 2015 7:39 AM

You can use just like other libraries and need to call it's method to get objectypecode. if you are looking for step by step instruction check this blog for how to step

XrmServiceToolkit:alisharifiblog.wordpress.com/.../getting-related-record-lookup-fields-with-retrieve-multiple-using-the-rest-endpoint-with-javascript

Thanks

Reply
Jishad Shajahan responded on 11 May 2015 8:56 AM

Hi HIMBAP,

I didn't want Sdk.Rest rather than that i want the details why

var lookupService = new RemoteCommand("LookupService","RetrieveTypeCode");

this is not working and what is the update for this one.

Also the code that you entered is not working, since the objcode is not assosiated with records, rather than it is assosiated with the Entity only.

Reply
Jishad Shajahan responded on 11 May 2015 8:57 AM

Hi Rajkumar,

I just want to get the objtype code using the javascript only.

Reply
Suggested Answer
Scott Durow (MVP) responded on 12 May 2015 3:28 AM

RemoteCommand is not part of the support SDK and so I am not surprised it does not work anymore.

You will need to make a call to the metadata to get the object type code - e.g. msdn.microsoft.com/.../gg594428.aspx

If you want and easy and (again unsupported) technique - you can use:

Mscrm.EntityPropUtil.EntityTypeName2CodeMap[entityName]

Hope this helps,

Scott

Reply
Verified Answer
Shekar M responded on 12 May 2015 4:15 AM

Hi Jishad,

In CRM 2015 updated rollup doesn't support the Remote command ,  you have to use supported methods only , 

XrmServiceToolkit provides a method named as RetrieveEntityMetadata function , use this as follows

var entityMetadata = XrmServiceToolkit.Soap.RetrieveEntityMetadata(["Entity"], "account", true);

in the result you will get the Object type code of account entity 

Reply
harihar responded on 12 May 2015 6:31 AM

Jishad, I am curious to know the reason why you are retrieving the Object type code for the entities.

thank you for the info.

harihar

Reply
Jishad Shajahan responded on 12 May 2015 8:29 AM

Scott,

How this code work with javascript.? can you explain in detail.

Reply
Jishad Shajahan responded on 12 May 2015 8:30 AM

To use along with Anotation entity, mainly used for document attachment.

Reply
Suggested Answer
Bob Hatcher responded on 13 May 2015 3:27 PM

Keep in mind Annotation uses an Option Set for the objecttypecode.

Here's a method I have in my personal toolkit that returns one column from one entity. You have to pass it a Fetch that returns only one row including the column you want.

function getSingleton (fetchQuery,attrib, isGUID, err)
{

	// fetchQuery is the Query in FETCHXML Format.
	// attrib is the logical name of the attribute you are returning, eg. accountid (schema name in lower case)
	// isGUID is a boolean that indicates if you are retrieving a value or a GUID.
	// err is an error message to be delivered to the user if there is an error. If you don't want an error, pass null.

	// Use to get one field from one record. Pass in a Fetch statement and the name of the attribute you want to return.
	// Requires XRM Service Toolkit.  http://xrmservicetoolkit.codeplex.com/

	var strAttrib = attrib.toString();
	var r;
	var returnArray = XrmServiceToolkit.Soap.Fetch(fetchQuery);
	if (returnArray == null) 
	{ return null; }

	// Check that you only got one thing back and that the field you requested exists.
	if ((returnArray.length == 1) && (returnArray[0].attributes[strAttrib] != undefined)) {
	   if (isGUID)
	   {
		r = returnArray[0].attributes[strAttrib].id;
	   } 
	   else
	   {
		  r= returnArray[0].attributes[strAttrib].value;
		}
	}
	else
	{
	   if (err != null) {
		   alert (err);
	   }
	   r= null;
	}
	return r;
}


XMLServiceToolkit must be on the form as a web resource.

In your case you could use it like this:

var fetch = 
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"  <entity name='annotation'>" +
"    <attribute name='objecttypecode' />" +
"    <attribute name='annotationid' />" +
"    <order attribute='subject' descending='false' />" +
"    <filter type='and'>" +
"      <condition attribute='annotationid' operator='eq' value = \""' + [GUID of annotation] + '\"" />" +
"    </filter>" +
"  </entity>" +
"</fetch>" ;

var annotationTypeCode = getSingleton(fetch,"objecttypecode",false,"There was an error");


Not tested or anything, but it should point you in the right direction.



Reply
srinumca04 responded on 6 Oct 2015 5:29 AM

Just use the class Mscrm.EntityPropUtil.EntityTypeName2CodeMap to get the entity type code.

Refer http://blog.hompus.nl/2015/05/28/entity-type-code-using-javascript-only/

Reply
Verified Answer
HIMBAP responded on 11 May 2015 5:08 AM

This is not a supported method, better to shift to supported methods. You can write a metadata query.

Check this: https://xrmservicetoolkit.codeplex.com/

Thanks

Reply
Verified Answer
HIMBAP responded on 11 May 2015 7:39 AM

You can use just like other libraries and need to call it's method to get objectypecode. if you are looking for step by step instruction check this blog for how to step

XrmServiceToolkit:alisharifiblog.wordpress.com/.../getting-related-record-lookup-fields-with-retrieve-multiple-using-the-rest-endpoint-with-javascript

Thanks

Reply
Verified Answer
Shekar M responded on 12 May 2015 4:15 AM

Hi Jishad,

In CRM 2015 updated rollup doesn't support the Remote command ,  you have to use supported methods only , 

XrmServiceToolkit provides a method named as RetrieveEntityMetadata function , use this as follows

var entityMetadata = XrmServiceToolkit.Soap.RetrieveEntityMetadata(["Entity"], "account", true);

in the result you will get the Object type code of account entity 

Reply
Suggested Answer
Rajkumar Rajaraman responded on 11 May 2015 4:55 AM

Hi Jishad,

Have u tried this link?

crmbusiness.wordpress.com/.../crm-20112013-javascript-to-get-the-object-type-code-of-an-entity

Alsp post your error screenshot

Regards,

R.Rajkumar

"Please mark my answer as verified if you found it helpful"

Reply
Suggested Answer
Rajkumar Rajaraman responded on 11 May 2015 6:08 AM

Hi Jishad,

Refer this thread for similar type of issue:

community.dynamics.com/.../67401

Hope this helps.

Regards,

R.Rajkumar

"Please mark my answer as verified if you found it helpful"

Reply
Suggested Answer
Scott Durow (MVP) responded on 12 May 2015 3:28 AM

RemoteCommand is not part of the support SDK and so I am not surprised it does not work anymore.

You will need to make a call to the metadata to get the object type code - e.g. msdn.microsoft.com/.../gg594428.aspx

If you want and easy and (again unsupported) technique - you can use:

Mscrm.EntityPropUtil.EntityTypeName2CodeMap[entityName]

Hope this helps,

Scott

Reply
Suggested Answer
Bob Hatcher responded on 13 May 2015 3:27 PM

Keep in mind Annotation uses an Option Set for the objecttypecode.

Here's a method I have in my personal toolkit that returns one column from one entity. You have to pass it a Fetch that returns only one row including the column you want.

function getSingleton (fetchQuery,attrib, isGUID, err)
{

	// fetchQuery is the Query in FETCHXML Format.
	// attrib is the logical name of the attribute you are returning, eg. accountid (schema name in lower case)
	// isGUID is a boolean that indicates if you are retrieving a value or a GUID.
	// err is an error message to be delivered to the user if there is an error. If you don't want an error, pass null.

	// Use to get one field from one record. Pass in a Fetch statement and the name of the attribute you want to return.
	// Requires XRM Service Toolkit.  http://xrmservicetoolkit.codeplex.com/

	var strAttrib = attrib.toString();
	var r;
	var returnArray = XrmServiceToolkit.Soap.Fetch(fetchQuery);
	if (returnArray == null) 
	{ return null; }

	// Check that you only got one thing back and that the field you requested exists.
	if ((returnArray.length == 1) && (returnArray[0].attributes[strAttrib] != undefined)) {
	   if (isGUID)
	   {
		r = returnArray[0].attributes[strAttrib].id;
	   } 
	   else
	   {
		  r= returnArray[0].attributes[strAttrib].value;
		}
	}
	else
	{
	   if (err != null) {
		   alert (err);
	   }
	   r= null;
	}
	return r;
}


XMLServiceToolkit must be on the form as a web resource.

In your case you could use it like this:

var fetch = 
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"  <entity name='annotation'>" +
"    <attribute name='objecttypecode' />" +
"    <attribute name='annotationid' />" +
"    <order attribute='subject' descending='false' />" +
"    <filter type='and'>" +
"      <condition attribute='annotationid' operator='eq' value = \""' + [GUID of annotation] + '\"" />" +
"    </filter>" +
"  </entity>" +
"</fetch>" ;

var annotationTypeCode = getSingleton(fetch,"objecttypecode",false,"There was an error");


Not tested or anything, but it should point you in the right direction.



Reply