Development, Howto

How to use SendEmailFromTemplate action with JavaScript and WebApi

var parameters = {
    TemplateId: "07B94C1D-C85F-492F-B120-F0A743C540E6",//Guid of template
    Regarding: {
        contactid: "CA26BD09-62B4-E711-A94F-000D3A109280",//entity that will be used for templating
        "@odata.type": "Microsoft.Dynamics.CRM.contact"
    },
    Target: {//stub of email that has to contain at least one valid recipient
        "@odata.type": "Microsoft.Dynamics.CRM.email",
        email_activity_parties: [{
            "partyid_contact@odata.bind": "/contacts(CA26BD09-62B4-E711-A94F-000D3A109280)",
            participationtypemask: 2//To
        }]
    }
};

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}

var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + "/api/data/v8.2/SendEmailFromTemplate", 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) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.