10. August 2015 15:09
function GetCaseEmailTemplate(TemplateId, EntityId) {
// Generate an email activity from the specified template
//
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\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type='InstantiateTemplateRequest' ReturnDynamicEntities='false' >"+
" <TemplateId>" + TemplateId + "</TemplateId>"+
" <ObjectType>incident</ObjectType>"+
" <ObjectId>" + EntityId + "</ObjectId>"+
" </Request>"+
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
//xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
var entityNode = resultXml.selectSingleNode("//BusinessEntityCollection/BusinessEntities/BusinessEntity");
if(entityNode == null)
{
alert("Could not read template.\n\nInformation for System Administrator:\n"+resultXml.text);
return;
}
// Get the email details
var emailBody = entityNode.selectSingleNode("q1:description");
var subject = entityNode.selectSingleNode("q1:subject");
document.all['descriptionIFrame'].contentWindow.document.body.innerHTML="<span style=\"font-size:10pt\; font-family:Arial;\">"+emailBody.text +"</span>"+ document.all['descriptionIFrame'].contentWindow.document.body.innerHTML;
if(Xrm.Page.getAttribute("subject").getValue()==null || Xrm.Page.getAttribute("subject").getValue().indexOf("Case ID: CAS")==-1){
Xrm.Page.getAttribute("subject").setValue(subject.text);
}
}