Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Email Signature not populated in the email description in MS CRM 2013

$
0
0

Hello All,

Can anyone help me solving the below issues? Any help is greatly appreciated.

Requirement: We need to set the Signature automatically when replying to the email messages (Email) in CRM 2013.

Approach 1: Written the below code and set it to run AddSignatureToEmail() onLoad of the Email form.

function AddSignatureToEmail() {

    // Pre-fill a template signature
    var drawSignature = false;                  
    var emailTemplateToLoad = "5E4DB80E-CF36-E611-8100-00155DAD0221";//Signature TemplateID
    // Check if description is blank or similar
    var theDescription = Xrm.Page.getAttribute("description").getValue();
    if (theDescription == null) {
        drawSignature = true;
    } else if (theDescription == undefined) {
        drawSignature = true;
    } else if (stripHTML(theDescription) == "") {
        drawSignature = true;
    } else if (stripHTML(theDescription).length < 10) {
        drawSignature = true;
    }
    if (drawSignature) {
        // Get user details
        var systemUserId = Xrm.Page.context.getUserId();
        if (systemUserId != null) {
            // Retrieving email template details
            var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
            command.SetParameter("templateId", emailTemplateToLoad);
            command.SetParameter("objectId", systemUserId);
            command.SetParameter("objectTypeCode", 8);
            var result = command.Execute();
            if (result.Success) {
                var o = new Object();
                o.EmailBody = "";
                o.EmailSubject = "";
                if (typeof (result.ReturnValue) == "string") {
                    // Create a Xml Document of the return value to retrieve actual data
                    oXml = CreateXmlDocument(result.ReturnValue);
                    o.EmailBody = oXml.lastChild.lastElementChild.textContent;

                    Xrm.Page.getAttribute("description").setValue(o.EmailBody);
                    Xrm.Page.getAttribute("description").setSubmitMode("always");
                }
            }
        }
    }
}
function stripHTML(signature) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = signature;
    return tmp.textContent || tmp.innerText;
}
function CreateXmlDocument(signatureXmlStr) {
    // Function to create Xml formate of return email template data
    var parseXml;
    if (window.DOMParser) {
        parseXml = function (xmlStr) {
            return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
        };
    }
    else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
        parseXml = function (xmlStr) {
            var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(xmlStr);
            return xmlDoc;
        };
    }
    else {
        parseXml = function () { return null; }
    }
    var xml = parseXml(signatureXmlStr);
    if (xml) {
        return xml;
    }
}

By using the above code I could get my signature email template body properly but couldn’t set the same to description field of Email entity.

Issues:

1a) I could get my signature email template body in o.EmailBody but couldn’t set that value to the ‘description’ field of Email. Signature not displayed after clicking on reply in the Email form. However, the signature is set accordingly when it is a new form (onLoad).

1b) In some emails, the signature was seen after clicking on reply in the Email form and that too in Google Chrome only.

1c) Observed that the email description field is always giving empty value (null) when the value is get using Xrm.Page.getAttribute(“description”).getValue();
========================================================================
Approach 2:

http://www.inogic.com/blog/2015/07/adding-signature-to-emails-in-dynamics-crm/#comment-1093

Used the above same logic but stuck up with the below issues.

Issues:

2a) Couldn’t get any description or subject field for var ‘result’ and couldn’t set the value to Email description.

2b) How to get the description of the email template from the var ‘result’ and assign the same to description field of Email? I could see my signature template information in the ‘result’ but it is not proper.

2c) If this script is triggered on OnLoad of the email form, – will it not effect the user adding the signature only if the user wants to open the form. The user should only see the signature while replying to the email and append it to the existing description of the email.



Thanks,

Srini

                    

Viewing all articles
Browse latest Browse all 8223

Trending Articles