locked
Email Signature not populated in the email description in MS CRM 2013 RRS feed

  • Question

  • 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

                        
    • Edited by K_Srinivas Wednesday, June 22, 2016 10:43 AM
    Wednesday, June 22, 2016 10:41 AM

All replies

  • Hi All,

    I could resolve this issue.

    Issue Background: When the Email form is loading, the description field is taking time (may be loading asynchronously) which is making our statements not work (intermittently working).

    Work Around: set Timeout for a second/until the value in the description is loaded and then set the value to the field which will work. See below for the code snippet.

    function SetEmailDescription(description)
    {
        var emailDescription = Xrm.Page.getAttribute("description").getValue();
        if (emailDescription == "")
            //Wait for 1 sec to wait and try again
            setTimeout(function () { SetEmailDescription(description); }, 1000);
        else
            description = description + emailDescription; // signature + old emails for reply emails
            Xrm.Page.getAttribute("description").setValue(description);
    }

    Thanks,

    Srini

    Wednesday, August 3, 2016 7:25 AM