Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Change Email Template with Javascript

(0) ShareShare
ReportReport
Posted on by 1,067

Hi All,

I have a business requirement on which due to the change of certain field in a custom entity, a new email window should appear. This email has to autopopulate the "To" field with users from different entities and change the Email Template based on some rules defined. Everything is working fine so far but I'm stuck on changing the email template using JavaScript. So I was wondering, what's the best option to do this on D365? 

Thanks in advance.

*This post is locked for comments

  • dvn92 Profile Picture
    65 on at
    RE: Change Email Template with Javascript

    Hi Anwesh Reddy Thati

    Thank you this looks good, however, in my scenario I am using data fields in my email template (for example to show the case reference number). I can see that with this code my template text loads up until the point of the first data field then nothing else loads after that so I think the code is not handling this or it's not possible to use data fields and insert the template via this JS method.

    I was hoping this JS could be used as an alternative to agents having to select "insert template" and fill out the system dialogue box by choosing regarding etc.

    If you think of a solution let me know - I appreciate your time! 

  • Anwesh Reddy Thati Profile Picture
    15 on at
    RE: Change Email Template with Javascript

    extended Vels's solution. This should work in all browsers.

    function LoadcustomTemplate() {
    
    		var emailTemplateToLoad = "G545D9CD-B5E7-EA11-81A1-FA163E6EHHA1"; // Template ID   
       		var select = "?$select=body";
            var filter = "&$filter=templateid eq "   emailTemplateToLoad;
            var fullUrl = Xrm.Page.context.getClientUrl()   "/api/data/v8.2/templates/"   select   filter;
    
            var retrieveRecordsReq = new XMLHttpRequest();
            retrieveRecordsReq.open("GET", fullUrl, false);
            retrieveRecordsReq.setRequestHeader("Accept", "application/json");
            retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveRecordsReq.send();
            
            if (retrieveRecordsReq.status === 200) {
                var parsedObjects = JSON.parse(retrieveRecordsReq.responseText);
                if (parsedObjects !== null && parsedObjects.value.length > 0) {            
               		var bodyvalue = parsedObjects.value[0]['body'];
    				var defaultBody = "";
    				if((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true)){
                  	var doc = new ActiveXObject("Microsoft.XMLDOM");
                    doc.preserveWhiteSpace = true;
                    doc.async = false;
                    doc.loadXML(bodyvalue);
    				defaultBody = doc.text;
    				}
    				else{
    					var parser = new DOMParser();
    					var xmlDoc = parser.parseFromString(bodyvalue, "text/xml");
    					var cData = xmlDoc.getElementsByTagName("xsl:template")[0].childNodes[0];
    					defaultBody = cData.data;
    				}
                    Xrm.Page.getAttribute("description").setValue(defaultBody);            
                }
             }       
        }

  • dvn92 Profile Picture
    65 on at
    RE: Change Email Template with Javascript

    Vels This looks like exactly what I need exept it uses ActiveXObject which only works on IE. Do you happen to have an alternative that works with Chrome/Firefox maybe using the DOMParser() method? I have tried adding DOMParser() myself but I'm having some difficulty

  • Suggested answer
    Vels Profile Picture
    5 on at
    RE: Change Email Template with Javascript

    Hi, you can insert / change the template in description via JavaScript.

     function LoadcustomTemplate() {
    
    		var emailTemplateToLoad = "G545D9CD-B5E7-EA11-81A1-FA163E6EHHA1"; // Template ID   
       		var select = "?$select=body";
            var filter = "&$filter=templateid eq "   emailTemplateToLoad;
            var fullUrl = Xrm.Page.context.getClientUrl()   "/api/data/v8.2/templates/"   select   filter;
    
            var retrieveRecordsReq = new XMLHttpRequest();
            retrieveRecordsReq.open("GET", fullUrl, false);
            retrieveRecordsReq.setRequestHeader("Accept", "application/json");
            retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveRecordsReq.send();
            
            if (retrieveRecordsReq.status === 200) {
                var parsedObjects = JSON.parse(retrieveRecordsReq.responseText);
                if (parsedObjects !== null && parsedObjects.value.length > 0) {            
               		var bodyvalue = parsedObjects.value[0]['body'];           		
                  	var doc = new ActiveXObject("Microsoft.XMLDOM");
                    doc.preserveWhiteSpace = true;
                    doc.async = false;
                    doc.loadXML(bodyvalue);
                    Xrm.Page.getAttribute("description").setValue(doc.text);            
                }
             }       
        }

  • EnriqueMdz Profile Picture
    1,067 on at
    RE: Change Email Template with Javascript

    Hi Kokulan Eswaranathan, thanks for your answer. The main problem is that I need to insert the template before the actual creation of the email.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at
    RE: Change Email Template with Javascript

    Hi

    You could create a custom action and pass the email template as the parameter to it and create the email record in the action and then return the email Id as the output parameter.

    In your Javascript, call the action, pass the email template name or id as the parameter and use the returned email record GUID and use Xrm.Navigation.openForm to open the email record. Please see below for links

    Creating a Custom Action

    www.youtube.com/watch

    Calling Custom Action in JS

    community.dynamics.com/.../230656

    Opening the email record created

    docs.microsoft.com/.../openform

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at
    RE: Change Email Template with Javascript

    You can write a plugin or workflow to do this.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans