web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Create Email Activity from Template using JavaScript?

(0) ShareShare
ReportReport
Posted on by 1,505

Hi there,

I wonder if there is a way to get the e-mail template data via the REST call and setup the description to e-mail body.
Please share your thoughts.
Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Have you tried to do that using WebApi? Here are helpers that can make development of code much more easier for you - github.com/.../JS

  • Verified answer
    ClixSol Profile Picture
    1,505 on at

    I found and It works, code is below just pass the parameter Template guid and call "LoadTemplate" function on Email Load form :
    function LoadTemplate() {
    // Check the form type should be 'create' type

    var emailTemplateToLoad = "358497C8-F902-E611-80F1-3863BB2E92C8"; //created email template Id

    // Get Regarding object details
    var regardingobjectid = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingobjectid == undefined) { return; }
    var RegardingItems = Xrm.Page.getAttribute("regardingobjectid").getValue();

    // The signature will not append if regarding field remains blank.
    if (RegardingItems != null) {
    var regardingObjectId = RegardingItems[0].id;
    var regardingObjectType = RegardingItems[0].type;

    if (regardingObjectId == null || regardingObjectId == "") { return; }
    if (regardingObjectType == null || regardingObjectType == "") { return; }

    // Retrieving email template details

    var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
    command.SetParameter("templateId", emailTemplateToLoad);
    command.SetParameter("objectId", regardingObjectId);
    command.SetParameter("objectTypeCode", regardingObjectType);
    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);
    Xrm.Page.getAttribute("subject").setValue(oXml.lastChild.firstElementChild.textContent);
    Xrm.Page.getAttribute("description").setValue(oXml.lastChild.lastElementChild.textContent);
    }
    }
    }
    }

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

     

  • Community Member Profile Picture
    on at

    Hi,

    We have tried the above-mentioned procedure to retrieve the email content. But we are facing issue while using  oXml.lastChild.lastElementChild.textContent. We are able to retrieve the hard-coded content. But the dynamic data from entities is not getting populated. We have tried innerHTML / outerHTML, but they didn't work. Do we have to retrieve the data related to the entity separately and append it to email content? Or have you faced a similar issue and there is a workaround solution for this. 

    Thanks,

    Sridhar. 

  • Jeffrey O Profile Picture
    70 on at

    I cant get this to work in D365 online. Keeps failing at var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate"); Did this change that you know of ?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans