Our next customization is a toolbar button that enables a user to make copies of records with the click of a button. The purpose of this clone feature is to expedite the process of entering multiple records at once when the records are all very similar to one another. For example, if we are manually building our product list and have many similar items we can use this functionality to quickly copy records so we can then make changes to the copied record.
Let's take a closer look at this customization. As with any other customization, it is necessary to make sure that the ISV Integration key in the web.config file is turned on. Next, we need to add the following line in the Entities section in the isv.config file to add the clone button on the menu bar of the different entities we want to be able to clone:
<account> <ToolBar ValidForCreate="0" ValidForUpdate="1"> <Button Title="Clone" ToolTip="Clone this account" Icon="/_imgs/ico_18_debug.gif" Url="/Custom/c360/ToolPac/Clone.aspx" PassParams="1" WinParams="" WinMode="1" /> </ToolBar> </account>
NOTEOur clone customization is capable of cloning accounts, contacts, opportunities, leads, cases, competitors, quotes, orders, and invoices. |
When the user clicks the new clone button, a new window pops up and loads the Clone.aspx page. This page retrieves the original record, makes a copy of it, adds Clone of to the name or description of the new record, and displays a confirmation message. When the user dismisses the confirmation, the current window is closed, the new record is diplayed in a new pop-up window, and also the grid on the main screen is refreshed so that the new record is displayed if needed.
Looking at Clone.aspx, and more specifically at the Page_Init() method, you see that we first read the unique Id and the type of the original object we want to clone:
string strObjectId = Request.QueryString["oId"].ToString(); string strObjectType = Request.QueryString["oType"].ToString();
The reason those two values are passed to our custom ASPX page is that we turned on the PassParams option in ISV.config. The object type is an integer that represents the object. For example, 1 is an account, 2 is a contact, and 3 is an opportunity. Table 14.3 includes a complete list of all the Microsoft CRM object types.
Some of the types listed in Table 14.4 are not yet implemented in Microsoft CRM, but will be in a future version. An example of this is the ChatActivity object. If the existence of a CRM object that involves chat piques your curiosity about things to come, be sure and read Chapter 21, “Future Directions of Microsoft CRM.”
Next, we need to identify the current user which is done by the following:
CBizUser objCurrentUser = new CBizUser(); CUserAuth objUserAuth = objCurrentUser.WhoAmI();
We must now retrieve all fields for the source object, which is exactly what the RetrieveObject method we wrote does by calling the Retrieve method of the appropriate CRM object. In the case of an account, for example, this is the code that performs the retrieval:
CRMAccount objAccount = new CRMAccount(); strResult = objAccount.Retrieve( ref objUserAuth, strObjectId, "");
The retrieve method expects three parameters: a reference to a UserAuth object returned by the WhoAmI method, the unique identifier of the object to retrieve, and the list of fields to retrieve.
NOTEThis list of fields must adhere to the ColumnsetXml, format, which is defined in the SDK in the ColumnSet XML Schema section. However, one thing that the SDK does not mention is that an empty string is the equivalent of saying, “I want all fields.” |
The last step in this customization is to take the string returned by the Retrieve method, filter out the fields that are not valid when creating a new object, change the description node (depending on the object type, this node can be the name, first name, description, subject, and so on) to say “Clone of…” so that this new record is clearly identified as a clone of another record, and finally create the new record. This is what the CloneRecord method does.
The rest of the code in the ASPX page is client-side JavaScript code to close the current window, open a new one, and display the new record.