Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

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

Posted on by 16
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;  
       }  
   );  
}
 
 

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans