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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Create Email with Template and Open in Modal using Javascript in D365

(0) ShareShare
ReportReport
Posted on by 70
0

I've been fighting trying to complete opening an email template instance with create form type for a bit. I found to be what a viable solution here: https://www.itaintboring.com/dynamics-crm/instantiating-an-email-template-with-web-api/

I've spent more than a full day trying various methods using remotecommand however it seems to be deprecated.

The following is my javascript:

function ltCreateIAafterPurchaseTemplate(executionContext, id){
    alert("IA AFTER PURCHASE");

    var formContext = executionContext;
    var serialNo = formContext.getAttribute("new_licenseno").getValue();
    var emailTemplateToLoad = "f22cb309-1fd9-eb11-bacb-000d3a361c51";
    var licenseId = id;
    licenseId = licenseId.toString().replace("{", "").replace("}", "");
    var endUserContactId = formContext.getAttribute("new_endusercontactid").getValue()[0].id;
    endUserContactId = endUserContactId.toString().replace("{", "").replace("}", "");
    
        var parameters = {
        "TemplateId": emailTemplateToLoad,//GUID
        "ObjectType": "new_license",//Entity logical name, lowercase
        "ObjectId": licenseId//record id for the entity above
    };
var requestUrl = "/api/data/v9.2/InstantiateTemplate";
var req = new XMLHttpRequest();
req.open("POST", "">dev.api.crm.dynamics.com" + requestUrl, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            //DO SOMETHING WITH THE RESPONSE HERE
            alert(result.value[0].subject);
            alert(result.value[0].description);
        } else {
            var errorText = this.responseText;
            alert(errorText);
        }
    }
};
req.send(JSON.stringify(parameters));}

The above code executes and receive the following 401 response:

{"error":{"code":"","message":"No HTTP resource was found that matches the request URI '">dev.api.crm.dynamics.com/.../InstantiateTemplate'."}}

I could use a boost of help as I've been fighting with this all night. All ids in the param list are valid.

Regards.

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    As the error message shows, your request URI is wrong. Please change it to your environment URL(Something like https://yourorgname.crm.dynamics.com) to have a try. You can get environment URL through the following steps.

    1. Login the Power Platform admin center (microsoft.com).

    2. Enter the environment you used and you would find it.

        environment.png

  • Suggested answer
    Sergi Valero Profile Picture
    145 on at

    Hello Jeffrey,

    As Steve noted, there is an error with the direction you are doing the request to. Another way you can get your Organization's  URL is with formContext.

    There is a method called "getClientUrl()" which will return the URL string you need to add before the requestUrl. Here you will find more info about the topic.

    formContext.getClientUrl();

    Hope it helps!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 175

#2
ManoVerse Profile Picture

ManoVerse 142 Super User 2026 Season 1

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 59 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans