Thursday, October 9, 2014

ERD Generator for Dynamics CRM 2011/2013

ERD Generator for Dynamics CRM 2011/2013 is a Windows Form application that is can be used for Dynamics CRM 2011 or Dynamics CRM 2013.
This tool allows you to build ERD (Entity relationship diagrams) that are dynamically updated based on the published CRM entities and Fields. You can pick a source entity and build ERD around it. It generate a VISIO output but also lets you pick CSV to document Entity relationships.

Thursday, July 24, 2014

Use of Shared Variables in Dynamics CRM 2011 Plugins

Introduction of Shared Variables in MS CRM2011 Plug-in might not been used by most of us, as we are not aware of its unique feature. This Shared Variables will be useful for sharing the data during complex plug-in development by sharing the data between plug-ins registered on both the pre and post events.

Any values can be stored in the plugin context of Pre-event will be available in the Post-event of the plugin. This way we can avoid storing the values in a custom attribute. Certain data validation which needs to be done at post-event can be achieved by passing the value from pre-event of the plugin. This also helps for performing business logics like updating related records asynchronously.

The below code snippet illustrates the usage of Shared Variable.  This sample explains the data validation done at post-event operation based on the value received from the pre-event operation where a flag is updated with a value at the pre-event level and sent to the post-event through shared variable.

Pre-Create (Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
                        {
                            if (pluginContext.InputParameters.ContainsKey("Target") && pluginContext.InputParameters["Target"is Entity)
                            {
                                Entity targetEntity = context.InputParameters["Target"as Entity;
                                if (targetEntity != null)
                                {
                                    bool setFlag = false;

                                    //Check data validation for attributes                                  
                                    string country = targetEntity.Attributes["address1_country"];
                                    OptionSetValue industryCode = (OptionSetValue)target.Attributes["industrycode"];
                                   
                                    // Country is US and Industry Type is Accounting are Preferred 
                                    if (country.ToLower() == "us" && industryCode.Value == 1)
                                    {
                                        setFlag = true;
                                    }                                   
                                    pluginContext.SharedVariables.Add("flag", setFlag);
                                }
                            }
                        }

Post-Create (Entity Name) Plug-in
if (pluginContext.MessageName == "Create")
                        {
                            if (pluginContext.InputParameters.ContainsKey("Target") && pluginContext.InputParameters["Target"is Entity)
                            {
                                Entity targetEntity = pluginContext.InputParameters["Target"as Entity;
                                if (targetEntity != null)
                                {
                                    if (pluginContext.SharedVariables.ContainsKey("flag"))
                                    {
                                        bool recievedFlag = (bool) pluginContext.SharedVariables["flag"];
                                        if (recievedFlag)
                                        {

                                        }
                                    }
                                }
                            }
                        }

This method lets you to pass data between plug-ins without having to customize the system by creating hidden fields on entities.

Note: SharedVariables (collection of key/value pairs) property is available in the IPluginExecutionContext of the execution pipeline.

For more help Click

Retrieve Linked Entity Column Data using Query Expression using MS CRM 2011 and MS CRM 2013 SDK

Most often you need to retrieve data from primary and related entity. This data can be retrieved using CRM SDK either with FetchXML or Query Expression.  In this blog, QueryExpression is used to retrieve primary and related entity data with a single database retrieve. Use AliasedValue to retrieve Column values from Linked Entity.
Below is the syntax of how related entity columns are retrieved.

Query Syntax:-

Parent Entity:-
var query = new QueryExpression(Entityname);
var columnNames = new[] { Columns to be Added };
query.ColumnSet = new ColumnSet(columnNames);

Linked Entity:-
var colsAccount = new[] { LinkEntity Columns to be Added }; // Related Entity Fields
LinkEntity linkentity = new LinkEntity() {
LinkFromEntityName = EntityName,
LinkFromAttributeName = EntityFieldName,
LinkToEntityName = LinkEntityName,
LinkToAttributeName = LinkEntityFieldName,
JoinOperator = JoinOperator.Inner,                        
Columns = new ColumnSet(colsAccount),
EntityAlias = AliasName
                        };
query.LinkEntities.Add(linkentity);

This sample shows how to retrieve related entities data using the RetrieveMultiple method using QueryExpression. The code returns columns from the primary entity Account as well as the firstname and lastname from associated Contact record.
Parent Entity:-

 var query = new QueryExpression("account");


 var columnNames = new[] { "fullname""accountid" };


 query.ColumnSet = new ColumnSet(columnNames);


 query.Criteria.AddCondition("accountid"ConditionOperator.Equal, new Guid(<<ENTER GUID HERE>>));
Linked Entity:-

var colsAccount = new[] { "firstname""lastname" };


                        LinkEntity linkEntityAccount = new LinkEntity()

                        {


                            LinkFromEntityName = "account",


                            LinkFromAttributeName = "primarycontact",


                            LinkToEntityName = "contact",


                            LinkToAttributeName = "contactid",


                            JoinOperator = JoinOperator.Inner,


                            Columns = new ColumnSet(colsAccount),


                            EntityAlias = "Contacts"


                        };


 query.LinkEntities.Add(linkEntityAccount);     
// Execute Query using RetrieveMultiple
//The RetrieveMultipleRequest is for returning multiple instances of a particular type of entity.
EntityCollection _results = _serviceproxy.RetrieveMultiple(query);
        if (_results != null)
             {
                foreach (var ent in _results.Entities)
               {
     // Display “First Name” along with Alias
     Console.WriteLine((ent.Attributes["Contacts.firstname"as AliasedValue).Value);
     // Display “Last Name” along with Alias
     Console.WriteLine((ent.Attributes["Contacts.lastname"as AliasedValue).Value);
                           
}
                        }

For more info please click

Friday, May 23, 2014

Cancel save event in MS CRM 2013

Use the below line of code to cancel the save event in CRM

Xrm.Page.context.getEventArgs().preventDefault();

or

e.getEventArgs().preventDefault();

e stand for to pass execution context in Java Script method.

Thursday, May 22, 2014

Attach OnClick event in Sub grid Records MS CRM 2013 Using JavaScript

Sometimes we have requirements to attach click event to sub-grid. So we could read selected record data and do the stuffs.

Here is sample code to attach click event to sub grid :

function ReadSelectedSubGridRecords() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        for (var rowNo = 0; rowNo < grid.get_selectedRecords().length; rowNo++)
            alert(grid.get_selectedRecords()[rowNo].Name);
    }
}

function Form_OnLoad() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName");
        // Google chrome
        if (grid.addEventListener) {
            grid.addEventListener('click', ReadSelectedSubGridRecords, false);
            // IE
        } else if (grid.attachEvent) {
            grid.attachEvent('onclick', ReadSelectedSubGridRecords);
        }

    }
    else {
        setTimeout("Form_OnLoad();", 2000);
    }
}

For more information please follow this BLOG

Read SubGrid Records in MS CRM 2013 using JavaScript

Many times we have requirements to read sub grid records and do subtotal or some others stuff with grid data. Here is same code to retrieve entire rows and columns from SubGrid.

function RetrieveSubGridRecords() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        for (var rowNo = 0; rowNo < grid.GetRecordsFromInnerGrid().length; rowNo++)
            for (var cellNo = 0; cellNo < grid.GetRecordsFromInnerGrid()[rowNo][3].cells.length; cellNo++)
                alert(grid.GetRecordsFromInnerGrid()[rowNo][3].cells[cellNo].outerText);
    }
    else {
        setTimeout("RetrieveSubGridRecords();", 2500);
    }

}


Read selected SubGrid Records in MS CRM 2013 using JavaScript.

function ReadSelectedSubGridRecords() {
        if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        for (var row = 0; row < grid.get_selectedRecords().length; row++)
            alert(grid.get_selectedRecords()[row].Name);
    }
}


Read sub-Grid cell value in MS CRM 2013 using Javascript.

function GetSubGridCellValues() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        var ids = gridControl.get_allRecordIds();
        for (i = 0; i < ids.length; i++) {
            alert(gridControl.getCellValue('fullname', ids[i]));
        }
    }
    else {
        setTimeout("GetSubGridCellValues();", 2500);
    }


}

for more info please follow this BLOG

Wednesday, May 21, 2014

Find account attributes based on account Id using SOAP request MS CRM 2011 and MS CRM 2013

I need to retrieve all the attribute from account entity based on  accountID.

sample code given below:

 function ExecuteRequest(_XML, Message) {
try {
var _ResultXML = null;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", Xrm.Page.context.getServerUrl() +"/XRMServices/2011/Organization.svc/web"false);
xmlhttp.setRequestHeader("Accept""application/xml, text/xml, */*");
xmlhttp.setRequestHeader("Content-Type""text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction","http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/" + Message);
xmlhttp.send(_XML);
_ResultXML = xmlhttp.responseXML;
var errorCount = _ResultXML.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
_ResultXML = null;
return _ResultXML;
}
else {
return _ResultXML;
}
}
catch (Err) {
alert(Err);
return;
}
}

function GetAccountName(accountID) {
var request = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<s:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
"<entityName>account</entityName>" +
"<id>"+ accountID +"</id>" +
"<columnSet xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
"<a:AllColumns>false</a:AllColumns>" +
"<a:Columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
"<b:string>name</b:string>" +
"<b:string>accountnumber</b:string>" +
"</a:Columns>" +
"</columnSet>" +
"</Retrieve>" +
"</s:Body>" +
"</s:Envelope>";
var _ResultXML = ExecuteRequest(request, "Retrieve");

var _AccountName = _ResultXML.selectSingleNode("//a:Attributes").selectSingleNode("//b:value").text;
return _AccountName;
}

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...