Please wait, loading...

 

Create and Send Email Message Activity from javascript using api D365 / MS CRM 2016

January 7, 2018
For Creating email message activity from java script MS CRM , you can update and use the below code according to your requirement. Here to show I have taken up a scenario to send a email to system user when a Quote is won.

Replace the User GUID for to and from user in “fromFieldIdSystemAdmin” and  “toPartyUsersId”

function CreateEmail() {
    var fromFieldIdSystemAdmin = "53536B3D-4D71-4FC2-857C-0477750A92BE"; //todo Email from User GUID
    var toPartyUsersId = ["53536B3D-4D71-4FC2-857C-0477750A92BE"]; //todo Email To user array
    var quoteId = Xrm.Page.data.entity.getId();
    quoteId = quoteId.replace(/[{}]/g, "");
  var customerLookUp = Xrm.Page.getAttribute("customerid");
    var accountName = null;
    if (customerLookUp != null)
        accountName = customerLookUp.getValue()[0].name;
    var webapiPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/emails";
    var email = {};
    //Lookup
    email["regardingobjectid_quote@odata.bind"] = "/quotes(" + quoteId + ")"; //Regarding is quote
    email["subject"] = "Quote Sent ";
    email["description"] = "See attachment for the Quote that was set as Won";
    var parties = [];
    //ActivityParty (From)
    var sender = {};
    sender["partyid_systemuser@odata.bind"] = "/systemusers(" + fromFieldIdSystemAdmin + ")";
    sender["participationtypemask"] = 1; //From
    parties.push(sender);
    for (i = 0; i < toPartyUsersId.length; i++) {
        var receiver = {};
        receiver["partyid_systemuser@odata.bind"] = "/systemusers(" + toPartyUsersId[i] + ")";
        receiver["participationtypemask"] = 2; //To
        parties.push(receiver);
    }
    email["email_activity_parties"] = parties;
    var service = new XMLHttpRequest();
    service.open("POST", webapiPath, false);
    service.setRequestHeader("Accept", "application/json");
    service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    service.setRequestHeader("OData-MaxVersion", "4.0");
    service.setRequestHeader("OData-Version", "4.0");
    service.send(JSON.stringify(email));
    if (service.readyState == 4) {
        if (service.status == 204) {
            var uri = service.getResponseHeader("OData-EntityId");
            var regExp = /(([^)]+))/;
            var matches = regExp.exec(uri);
            var emailGblId = matches[1];
            SendEmail(emailGblId);
        } else {

            Xrm.Utility.alertDialog(this.statusText);
        }
    }
}

function SendEmail(emailGblId) {
    var parameters = {};
    parameters.IssueSend = true;
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/emails(" + emailGblId + ")/Microsoft.Dynamics.CRM.SendEmail", 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 results = JSON.parse(this.response);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));
}

function successCallback() {
}
function errorCallback() {
}

Happy Coding 😊

SOURCE : mscrm.com

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651
Microsoft Dynamics Community Profile

Learn more