In this Post, I’ve used two custom entities “Flight Route” and “Airport”. I’ve launched a new Airport popup window (create mode) on click of the custom ribbon pop-up button called “Insert New Airport” located at “Flight Route” entity.
The Object Type Code (entity type code) of custom entities can differ when you move your customizations from development to Test, Acceptance or Production environments. Hence, you do not want to hard-code this Object Type Code anywhere in your code. This post will explain you how to fetch the Object Type Code of custom entity using JavaScript. Below are the steps for an entire implementation.
1. JavaScript Library as a web resource
· Create New Web Resource, this web resource will contain following two functions.
· Click on Text Editor.
· Insert below functions in Text editor window.
GetObjectTypeCode(entityName)
This is a generic method, a common method to fetch entity type code by passing entity name as parameter. Microsoft is also using a Remotecommand method to execute many of the ribbon actions.
CreateAirport()
This method will build the URL and open Airport entity record in create mode. It fetches the server url & entity type code and use both the values to build the required URL.
2. Insert custom ribbon pop-up button on your custom entity as shown in below link.
http://ankit.inkeysolutions.com/2012/02/crm-2011-ribbon-popup-buttons-via.html
Note:- While inserting the ribbon popup button, make sure you will change the “LaunchNewEntry.js” file name with above web resource file name, if you have changed the filename. You could quickly search the file name in exported customization.
3. Now you can verify the button on your entity’s form ribbon.
4. On click of this button your entry form will be launched.
The Object Type Code (entity type code) of custom entities can differ when you move your customizations from development to Test, Acceptance or Production environments. Hence, you do not want to hard-code this Object Type Code anywhere in your code. This post will explain you how to fetch the Object Type Code of custom entity using JavaScript. Below are the steps for an entire implementation.
1. JavaScript Library as a web resource
· Create New Web Resource, this web resource will contain following two functions.
· Click on Text Editor.
· Insert below functions in Text editor window.
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;
}
}
function CreateAirport()
{
// Get the CRM URL
var serverUrl = Xrm.Page.context.getServerUrl();
//Set features for how the window will appear
var features = "location=no,menubar=no,status=no,toolbar=no,width=700,height=600";
var ObjCode=GetObjectTypeCode('acm_airport');
var path = serverUrl + '/main.aspx?etc='+ObjCode+'&extraqs=%3fetc%3d'+ObjCode+'&pagetype=entityrecord';
//Pop the window
window.open((path), '_blank', features, false);
}
To open a new Entry form of Airport entity from Flight Route entity, first you’ve to construct URL and fetch Object Type Code of the Airport entity. These two JavaScript methods help in that need.
GetObjectTypeCode(entityName)
This is a generic method, a common method to fetch entity type code by passing entity name as parameter. Microsoft is also using a Remotecommand method to execute many of the ribbon actions.
CreateAirport()
This method will build the URL and open Airport entity record in create mode. It fetches the server url & entity type code and use both the values to build the required URL.
2. Insert custom ribbon pop-up button on your custom entity as shown in below link.
Note:- While inserting the ribbon popup button, make sure you will change the “LaunchNewEntry.js” file name with above web resource file name, if you have changed the filename. You could quickly search the file name in exported customization.
3. Now you can verify the button on your entity’s form ribbon.
4. On click of this button your entry form will be launched.
Hi Ankit
ReplyDeleteI haven't found any documentation about the RemoteCommand / LookupService.
Is that really supported?
If not - why not simply use the RetrieveEntityRequest class to obtain metadata in a supported way?
Regards
Jonas
Hi Jonas,
ReplyDeleteI don't think it is an unsupported way. The code here uses Microsoft’s own logic. Please refer below URLs for more details.
http://chaitanyaprasadtk.blogspot.in/2011/09/object-type-code-in-crm-2011.html
http://social.microsoft.com/Forums/en-PH/crmdevelopment/thread/6d52fff8-6ce7-43f5-abe7-c8aa9d72c70a
http://www.mscrmking.com/2011/07/how-to-get-the-entity-type-code-object-type-code-in-crm-2011-part-2/
Regards,
Ankit
GetObjectTypeCode(entityName) doesn't work if there is a htm web resource, do you know how to get object type code by entity name in the htm web resource?
ReplyDeleteTo me unsupported means that MS will not guarantee that this will work on future rollups or CRM upgrades. This is not in the SDK as and therefore unsupported. Use it if you want but accept the risks that the code may unexpectedly break and is unlikely to upgrade to the next major version.
ReplyDeleteOut of necessity and lack of features in older versions of the API I've written a lot of unsupported javascript (for CRM 3 and 4) but as soon as you upgrade you leave the client with a lot of code to rewrite and clean up.
Thanks for sharing, I personally would prefer to use a supported method whenever possible.