Senario :
select a record in crm 2011 grid view and click a ribbon button.
open the E-mail form with the selected record details as body(description).
for this first create on custom field in E-mail activity with "new_body"
create a javascript like this:
function emailopen(ids) {
// var selectedid = ids;
var entityname = 'new_cooldrink';
resultXml = RetrieveEntityById(entityname,ids, 'new_name');
var parameters = {};
parameters["subject"] = 'This is Subject';
parameters["new_body"] = resultXml.toString();
Xrm.Utility.openEntityForm("email", null, parameters);
}
//dont change any thing (for reference)
function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {
var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
arrayEntityColumns = prmEntityColumns.split(",");
for (var i = 0; i < arrayEntityColumns.length; i++) {
xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
}
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>" + prmEntityName + "</entityName>" +
"<id>" + prmEntityId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
xmlEntityColumns +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("Error Message : " + msg);
}
else {
return resultXml;
}
}
create a web resource and paste this code:
after that create ribbon button:
then write a javascript code in E-mail onload event:
function emailonload()
{
if (Xrm.Page.data.entity.attributes.get('test_body').getValue()!= null)
{
var selectedid = Xrm.Page.data.entity.attributes.get('new_body').getValue();
var entityname = 'new_cooldrink';
//var columns = 'new_name','new_productcount';
var x = null;
var y = null;
var resultXml = RetrieveEntityById(entityname, selectedid, 'new_name,new_productcount');
if (resultXml != null && resultXml.selectSingleNode('//q1:new_name') != null) {
x = resultXml.selectSingleNode('//q1:new_name').nodeTypedValue; //.getAttribute("name");
}
if (resultXml != null && resultXml.selectSingleNode('//q1:new_productcount') != null) {
y = resultXml.selectSingleNode('//q1:new_productcount').nodeTypedValue; //.getAttribute("name");
}
//var table = "<table border='1' bgcolor='#00FF00'><tr><td> Name </td> <td> Test </td> </tr><tr><td>" + x + "</td><td>" + y + "</td></tr></table>";
var z = x + y;
Xrm.Page.data.entity.attributes.get('new_body').setValue(z);
}
}
function xmlToString(resultXml) {
if ($(resultXml).find('q1\\:Attributes').length != 0) {
response = $(resultXml).find('q1\\:Attributes').eq(0);
} else {
response = $(resultXml).find('Attributes').eq(0); ; //Chrome 24.0.1312.52 could not find node by the previous code
}
var result = ((typeof window.CrmEncodeDecode != 'undefined') ? window.CrmEncodeDecode.CrmXmlDecode(response.text()) : crmXmlDecode(response.text()));
return result;
}
function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {
var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
arrayEntityColumns = prmEntityColumns.split(",");
for (var i = 0; i < arrayEntityColumns.length; i++) {
xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
}
var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
//Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>" + prmEntityName + "</entityName>" +
"<id>" + prmEntityId + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
xmlEntityColumns +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve></soap:Body></soap:Envelope>";
//call function to create Soap Request to ms crm webservice
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
resultXml = xmlHttpRequest.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("Error Message : " + msg);
}
else {
return resultXml;
}
}
for this code execution we need to add the
Jquery.js
Json2.js
XrmServiceToolkit.js in E-mail form.