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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Custom Command Button: Open Email Form and Auto-populating Email Body with Body from Email Template

(1) ShareShare
ReportReport
Posted on by 18
Hello;
 
I am trying to put together java code for a custom command button that will open an email form in a dialog. The email form will have the Regarding field, To field (same as regarding) and Subject line pre-populated from the Lead form. 
 
I am running into issues when I am trying to populate the email body with the content from an email template, but am having little success. The body of the email doesn't seem to be populating when the button is clicked. The fields seem to be populating as expected.
 
I found the id for the template (fd015726-c13a-ef11-840a-6045bd03efbc), but I require assistance with the java script. Can you please take a look at my code and let me know what I would need to chage/update in order to get the email body from the email template into the dialog form.
 
 
Please see my script below:
 
 
function OpenInitialEmail(primaryControl) {
 
    let formContext = primaryControl; // primaryControl parameter of the command button is same as formContext and NOT executionContext
 
   //Set template lookup parameter to pass to email
   var emailtemplate = {
       id: "fd015726-c13a-ef11-840a-6045bd03efbc",
       objectId: formContext.data.entity.getId(),
       objectTypeCode: "template"
    };
 
    // Define the data object with key-value pairs  
    var data = {
        // Populating Subject line  
        "subject": "Interest in HOOPP", 
        // Populating Dynamics Generated column  
        "sksysdev_dynamicsgenerated": true,
        // Populating To column with Lead 
        "to": [  
            {  
              "entityType": formContext.data.entity.getEntityName(),  
              "id": formContext.data.entity.getId(),  
              "name": formContext.data.entity.getPrimaryAttributeValue()
            }
          ],
    };
   
    // Define the parameters for the form  
   var pageInput = {  
       pageType: "entityrecord",  
       entityName: "email", // Target entity logical name
       data: data,
       templateid: emailtemplate,
       formId: "213b35fa-3e2e-ef11-840b-6045bd03efbc",     
       createFromEntity: { 
           "entityType": formContext.data.entity.getEntityName(),  
           "id": formContext.data.entity.getId(),  
           "name": formContext.data.entity.getPrimaryAttributeValue()  
       }  
   };  
 
   // Define the navigation options  
   var navOptions = {  
       target: 2,  
       height: {value: 80, unit:"%"},  
       width: {value: 70, unit:"%"},  
       position: 1  
   };  
 
   // Perform the navigation  
   Xrm.Navigation.navigateTo(pageInput, navOptions).then(  
       function success(result) {
        // Called when the dialog closes
    },  
       function (error) {   
               text: error.message;  
       }  
   );  
}
 
 
I have the same question (0)

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

News and Announcements

Season of Giving Solutions is Here!

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 > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#3
TAHER Mehdi Profile Picture

TAHER Mehdi 58

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans