Friday, 25 July 2014

Dynamics CRM 2013 / 2011 - On custom ribbon button click, create a new record, and open this newly created record


I’m feeling really happy while writing this blog, because it’s over a year that I got a chance to write it. I have been too busy for last year to write even a single blog. From now on, I’m hoping to keep this activity continued.

In this blog I would like to explain how to create a new record and open the same on click of custom ribbon button in Dynamics CRM. The steps are as following:

1. Create a custom ribbon button on a specific entity, the source entity. Use the Ribbon Workbench tool to add a button on this entity. This tool can be downloaded using following link.

2. Create a custom attribute of type of “Two Options” on the source entity say new_isribbonbuttonclicked. (specified above in step 1).

This field helps us in plug-in to identify whether the ribbon button is clicked by a user or not.

When the user clicks on this newly added button, a new record gets created via plug-in and the same will be opened in a new window with CRM 2011 or in a same window in CRM 2013.

3. Create another attribute of type “Single line text” on the same entity say new_uniqueidentifierofentity. This attribute will hold the GUID value of a newly created record.

The value of this field will be used in JavaScript/web resource code to open the newly created


Create a new web resource of type Script (JScript) and write the following JavaScript code in it. This code updates the value as “True” to the new_isribbonbuttonclicked attribute.

   1: //Change the method and attribute name as per your need
   2: function UpdateAttribute() {
   3:   var attribute = Xrm.Page.getAttribute("new_isribbonbuttonclicked"); //Specify the name of your attribute
   4:   var attributeValue = null;
   5:   if (attribute != null) {
   6:     attributeValue = attribute.getValue();
   7:     if (attributeValue == null ||
   8:         attributeValue == false) {
   9:       attribute.setValue(true);
  10:       attribute.setSubmitMode("always");
  12:     }
  13:   }
  14: }

5. Call the above JavaScript function from the ribbon button which we have created at very first step of this post.

6. Now, write a pre update plug-in on the source entity. In this plug-in just check whether the plug-in context contains the new_isribbonbuttonclicked attribute or not. If it exists in the context and the value of this attribute is “True” then create a record of another entity and get the unique identifier (GUID) of the newly created record from the response of the SDK call.

7. Then, add this GUID to the plug-in context for the new_uniqueidentifierofentity field.


entity.Attributes[new_uniqueidentifierofentity] = GUID; //Specify the name of your attribute

8. Also add the new_isribbonbuttonclicked field in the plug-in context as “False”. So that, the custom ribbon button will work for the next attempt.

9. At the end of plug-in execution the above attribute values get automatically updated.

10. Lastly, add another method in the web resource of the source entity and bind this method to “form load” event. Primarily, this method checks following two conditions.

· The form is loaded into the edit mode or not.

· The new_uniqueidentifierofentity attribute contains data or not.

If it satisfies both the conditions, meant if the record is opened in edit mode and the GUID attribute contains unique identifier of newly created record then just write a code to open this record, update the value of new_uniqueidentifierofentity to null, and save the form.

Following is the code that performs the things that we specified here in this step.

   1: //Change the method and attribute name as per your need
   2: function Form_Load() {
   3:   if (Xrm.Page.ui.getFormType() == 2) {
   4:     var attribute = Xrm.Page.getAttribute("AttributeName"); //Specify the name of your attribute
   5:     var attributeValue = null;
   6:     if (attribute != null) {
   7:       attributeValue = attribute.getValue();
   8:       if (attributeValue != null &&
   9:           attributeValue != "") {
  10:         Xrm.Utility.openEntityForm("EntityName", attributeValue);
  11:         attribute.setValue(null);
  12:         attribute.setSubmitMode("always");
  14:       }
  15:     }
  16:   }
  17: }

That’s it!

Hope, this helps. Thanks.