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

In this post 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 or by import export the solution to add a button on this entity.

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 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.

//Change the method and attribute name as per your need
    function UpdateAttribute() 
{
     var attribute = Xrm.Page.getAttribute(\”new_isribbonbuttonclicked\”); //Specify the name of your attribute
     var attributeValue = null;
      if (attribute != null) 
 {
       attributeValue = attribute.getValue();
       if (attributeValue == null ||
           attributeValue == false) 
{
         attribute.setValue(true);
         attribute.setSubmitMode(\”always\”);
         Xrm.Page.data.entity.save();
}
      }
    }


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.

    //Change the method and attribute name as per your need
    function Form_Load() {
      if (Xrm.Page.ui.getFormType() == 2) {
        var attribute = Xrm.Page.getAttribute(\”AttributeName\”); //Specify the name of your attribute
       var attributeValue = null;
       if (attribute != null) {
          attributeValue = attribute.getValue();
         if (attributeValue != null &&
              attributeValue != \”\”) {
           Xrm.Utility.openEntityForm(\”EntityName\”, attributeValue);
           attribute.setValue(null);
           attribute.setSubmitMode(\”always\”);
           Xrm.Page.data.entity.save();
        }
       }
     }
   }

That’s it!

Hope, this helps. Thanks.

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

3 thoughts on “Dynamics CRM 2013 / 2011 – On Custom ribbon button click, create a new record, and open this newly created record

  1. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

  2. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time. Contact him now and thank me later.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: