Monday 28 July 2014

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.


Thursday 10 July 2014

Convert QueryExpression to FetchXML and FetchXML to QueryExpression in MSCRM

It might a very common requirement that sometimes you need to convert FetchXMl to QueryExpression and Vice-Versa. Today i am going to share the same with all of you.

Query Expression to Fetch XML :

QueryExpression query = new QueryExpression(); 
            query.EntityName = "contact";
            query.ColumnSet = new ColumnSet(true);

            ConditionExpression cond = new ConditionExpression();
            cond.AttributeName = "firstname";
            cond.Operator = ConditionOperator.Equal;
            cond.Values.Add("Varun Singh");


            // Create Request
            QueryExpressionToFetchXmlRequest Queryexp_to_Fetch_req = new QueryExpressionToFetchXmlRequest();

            Queryexp_to_Fetch_req.Query = query;

            //Get Response
            QueryExpressionToFetchXmlResponse Queryexp_to_Fetch_resp = (QueryExpressionToFetchXmlResponse)service.Execute(Queryexp_to_Fetch_req);

            //work with newly formed fetch string
            string myfetch = Queryexp_to_Fetch_resp.FetchXml;


Fetch XML to Query Expression :


FetchXmlToQueryExpressionRequest fetch_to_QueryExp_req = new FetchXmlToQueryExpressionRequest();

            fetch_to_QueryExp_req.FetchXml = myfetch;

            FetchXmlToQueryExpressionResponse fetch_to_QueryExp_resp = (FetchXmlToQueryExpressionResponse)service.Execute(fetch_to_QueryExp_req);

            QueryExpression myquery = fetch_to_QueryExp_resp.Query;




Hope it will help someone........



















Blogger Widgets