Showing posts with label Web Resources. Show all posts
Showing posts with label Web Resources. Show all posts

Saturday, 26 July 2014


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. http://www.develop1.net/public/page/Ribbon-Workbench-for-Dynamics-CRM-2011.aspx  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.
   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: }



4. Call the above JavaScript function from the ribbon button which we have created at very first step of this post.
5. 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.
6. 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
7. 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.
8. At the end of plug-in execution the above attribute values get automatically updated.
9. 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");

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

  14:       }

  15:     }

  16:   }

  17: }



That’s it!
Hope, this helps. Thanks.

Saturday, 16 March 2013

Dynamics CRM 2011 - Propagate the plug-in invalid exception message in JavaScript code.

Recently I came across an issue where I would like to propagate the plug-in InvalidPluginExcecutionException message via JavaScript code. Based on the client requirements I need to trigger a plug-in on click of custom ribbon button on the Opportunity entity. Hence on custom button click I have written a web service call that updates one of custom attributes of an Opportunity entity. So, when system will update this field via client side web service call, the registered plug-in will be triggered automatically. Usually your InvalidPluginExcecutionException message inside your plug-in code is displayed to user in a standard CRM message box. In my case I am unable to see the same because the plug-in is being triggered via JavaScript web service call. Below is the code snippet that executes my query.
//Asynchronous AJAX function to update a CRM record using OData

  $.ajax({

    type: "POST",

    contentType: "application/json; charset=utf-8",

    datatype: "json",

    data: jsonEntity,

    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",

    beforeSend: function (XMLHttpRequest) {

      //Specifying this header ensures that the results will be returned as JSON.             

      XMLHttpRequest.setRequestHeader("Accept", "application/json");

 

      //Specify the HTTP method MERGE to update just the changes you are submitting.             

      XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");

    },

    success: function (data, textStatus, XmlHttpRequest) {

      //The MERGE does not return any data at all, so we'll add the id 

      //onto the data object so it can be leveraged in a Callback. When data 

      //is used in the callback function, the field will be named generically, "id"

      data = new Object();

      data.id = id;

      if (successCallback) {

        successCallback(data, textStatus, XmlHttpRequest);

      }

    },

    error: function (XmlHttpRequest, textStatus, errorThrown) {

      if (errorCallback) {

 

        errorCallback(XmlHttpRequest, textStatus, errorThrown);

      }

      else {

        ErrorHandler(XmlHttpRequest, textStatus, errorThrown);

      }

    }

  }); 


I have validated all the 3 objects i.e. XmlHttpRequest, textStatus and errorThrown inside errorCallback method to seek the plug-in error message with no success. However I can see the same error message via fiddler!!
Finally, I found a way to parse the response text of xmlHttpRequest and get the exact error message. Below is the code that works for me to alert the plug-in message from client side code.


function ErrorHandler(xmlHttpRequest, textStatus, errorThrown) {

 

  alert(JSON.parse(xmlHttpRequest.responseText).error.message.value);

 

}


I hope this will be helpful.