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;
}

Read all Notes having attachment related to case entity in MS CRM 2011 and MS CRM 2013

Here is the sample code.

public static EntityCollection GetAllNotes(Guid CaseId, IOrganizationService service)
{
EntityCollection results = null;
try
{

QueryExpression Query = new QueryExpression
{
EntityName = "annotation",
ColumnSet = new ColumnSet("filename""documentbody""filesize""mimetype","isdocument"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,

Conditions =
{
new ConditionExpression
{
AttributeName = "objectid",
Operator = ConditionOperator.Equal,
Values = { CaseId }
},
new ConditionExpression
{
AttributeName = "isdocument",
Operator = ConditionOperator.Equal,
Values = { true }
},
}
}
};

results = service.RetrieveMultiple(Query);

}
catch (Exception Ex)
{
Console.Write(Ex.Message);
if (Ex.InnerException != null)
{
Console.WriteLine(Ex.InnerException.Message);
Console.ReadLine();
}
}
return results;

}

How to get/set Form header field in MS CRM 2011 and MS CRM 2013 !!!

There is no supported method in SDK to get and set form header field value using JavaScript. However, there is a workaround to achieve this just place field in Form Header and Form as well (You can hide field from Form in case of you don’t want to show the user). Then you can get and set the field values.

// to get field value
var Value=Xrm.Page.data.entity.attributes.get("new_field").getValue();

// to set field valuenew_field
Xrm.Page.data.entity.attributes.get("new_field").setValue(100);

Tuesday, May 20, 2014

How to set the value of Boolean attribute in MS CRM 2013

Xrm.Page.getAttribute("alb_trigger").setValue(true);

OR


Xrm.Page.getAttribute("alb_trigger").setValue(1);

Why window.showModalDialog does not refresh

Problem
I have some pages that is opened by window.showModalDialog().  But why is it not refreshed?
  • For example, when there is a compile error in the page, it will show it when opening it.  This is correct.  But after I correct the error, it will still show the same error.  I have to terminate ASP.NET worker process by issuing "iisreset" command.  Then it will show the corrected page.
  • Or when some data is changed.  When I open that page, it will still show the old data.  Like it is using the browser's local cache.  After I clear the brower's cache, then it will show the updated data.
Solution

In IE7, go to Tools  |  Internet Options.  Click the Browsing History "Settings" button.  Check for newer versions of stored pages: and select "Everytime I visit the page"

Wednesday, May 14, 2014

Open an entity record in a new window in MS CRM 2013 by Javascript

In CRM 2013 opening an entity record differs from previous versions. While CRM 2011 was all about popup windows CRM 2013 has a flat HTML5 navigation style. But you can still open a record in a popup window.
To open an entity record in a new window pass the following query string parameters.
etnThe logical name of the entity
extraqsOptional for forms (see Microsoft Dynamics CRM SDK for 2013 documentation)
pagetypeentityRecord
idThe id of the entity
newWindowtrue (undocumented)
histKeya random numeric sequence (undocumented)
The following sample shows the URL and query string parameters to open an account record in a new window:
window.open("https://contoso.crm.dynamics.com/main.aspx?etn=account&extraqs=&histKey=469645694&id={02c2e648-0acd-41f0-9ca8-d8d131e5f47b}&newWindow=true&pagetype=entityrecord","","status=0,resizable=1,width=1000px,height=600px");
click here for more info.

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