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.

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

record.

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");

11: Xrm.Page.data.entity.save();

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.

Like,

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

To call a plug-in from a custom ribbon button, create a new custom attribute on a form, register a plug-in either Create or Update messages as per your need for the required entity and keep a very first condition in plug-in that there is a change in this custom attribute.

Lastly on button click, write JavaScript code to change the value of this custom attribute and save a record from there.

Very helpful information. I do have one question as I'm implementing this logic. I have created a new custom ribbon button to call javascript to update a custom attribute (i.e. readytoresolve) to "true". I have a plugin setup to run on Update of Incident entity and will check for the value of the readytoresolve value to be changed. If so, the plugin will create an incident resolution and resolve the current case. I would like to be able to set the value of readytoresolve back to "false" in the plugin, but it doesn't seem to work. Any thoughts?

To set the value of readytoresolve back to "false" you need to change that in plug-in itself once the code is done with creating an incident resolution and resolving the current case.

There are two ways here to make this attribute false. 1. If you've written the plug-in on Pre event then set the value of the variable as “false” at the end in that plug-in itself. 2. If you’ve written the plug-in on Post event then you need to do one more update for the same entity to update the value of the variable as “false”.

Make sure you’ve very first condition in your plug-in code as the value of this variable should be “true”.

About Me

Ankit Shah is highly motivated and experienced professional, Bachelor Science in Information Technology -B.Sc. (I.T.) securing more than 8 years of IT experience on software design and development.
Presently Ankit is working as a Dynamics CRM consultant at Inkey Solutions, Surat, India.