Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Code to get the object Type Code in 2015 update 1

(0) ShareShare
ReportReport
Posted on by

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

*This post is locked for comments

  • naZir Profile Picture
    850 on at
    RE: Code to get the object Type Code in 2015 update 1

    Try this .. it is working :)

    [Organisational URL]/api/data/v8.2/EntityDefinitions?$select=LogicalName,ObjectTypeCode&$filter=ObjectTypeCode%20gt%209999

  • srinumca04 Profile Picture
    370 on at
    RE: Code to get the object Type Code in 2015 update 1

    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/

  • Suggested answer
    Bob Hatcher Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

    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.



  • Community Member Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

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

  • Community Member Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

    Scott,

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

  • Community Member Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

    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

  • Verified answer
    Soma Shekar Profile Picture
    461 on at
    RE: Code to get the object Type Code in 2015 update 1

    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 

  • Suggested answer
    ScottDurow Profile Picture
    19 on at
    RE: Code to get the object Type Code in 2015 update 1

    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

  • Community Member Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

    Hi Rajkumar,

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

  • Community Member Profile Picture
    on at
    RE: Code to get the object Type Code in 2015 update 1

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,342 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,027 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans