Create and Send Email using Web API in D365 CE(CRM)

In this blog, we will look at how we can create email activity records and send them using Web API in D365 CE(CRM).

Implementation

Creating Email

  1.  Creating Email with resolved emails (To field of email is record in D365 CE).
function createEmail() {
    var data = {
        "description": "Email Created",   ///Email Body
        "regardingobjectid_account@odata.bind": "/accounts(1f859c58-ad46-eb11-a813-
         000d3a1badf5)",
        "subject": "Test Email Creation",
        "email_activity_parties": [
            {
                "partyid_systemuser@odata.bind": "/systemusers(b411b085-ea3c-eb11-
                  bf68-000d3a197c1d)",
                "participationtypemask": 1   ///From Email
            },
            {
                "partyid_account@odata.bind": "/accounts(1f859c58-ad46-eb11-a813- 
                 000d3a1badf5)",
                "participationtypemask": 2 ///To Email
            }
        ]
    };
    Xrm.WebApi.createRecord("email", data).then(
        function success(result) {
            console.log("Email created with ID: " + result.id);
            // perform operations on record creation
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
}
  1.  Creating Email with unresolved emails (To field of email is not record in D365 CE).
function createEmail() {
    var data = {
        "description": "Email Created",   ///Email Body
        "regardingobjectid_account@odata.bind": "/accounts(1f859c58-ad46-eb11-a813-
         000d3a1badf5)",
        "subject": "Test Email Creation",
        "email_activity_parties": [
            {
                "partyid_systemuser@odata.bind": "/systemusers(b411b085-ea3c-eb11-
                  bf68-000d3a197c1d)",
                "participationtypemask": 1   ///From Email
            },
             {
                "addressused":"abc@gmail.com",
                 "participationtypemask" : 2 ///To Email
             }
        ]
    };
    Xrm.WebApi.createRecord("email", data).then(
        function success(result) {
            console.log("Email created with ID: " + result.id);
            // perform operations on record creation
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
}
  1. Creating Email with multiple unresolved emails (To field of email is not record in D365 CE).
function createEmail() {
    var data = {
        "description": "Email Created",   ///Email Body
        "regardingobjectid_account@odata.bind": "/accounts(1f859c58-ad46-eb11-a813-
         000d3a1badf5)",
        "subject": "Update Your Phone Number",
        "email_activity_parties": [
            {
                "partyid_systemuser@odata.bind": "/systemusers(b411b085-ea3c-eb11-
                  bf68-000d3a197c1d)",
                "participationtypemask": 1   ///From Email
            },
              {
                "addressused":"abc@gmail.com",
                 "participationtypemask" : 2   ///To Email
             },
             {
                "addressused":"xyz@gmail.com",
                 "participationtypemask": 2  ///To Email
             }
        ]
    };
    Xrm.WebApi.createRecord("email", data).then(
        function success(result) {
            console.log("Email created with ID: " + result.id);
            // perform operations on record creation
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
}

Sending Email

Parameter To be send in body:

Parameter Value Comment
IssueSendFalse If the value is set as false, then the email is marked as sent only in MS CRM.
IssueSendTrue
If the value is set as true, then the email is sent and as well as marked as sent in MS CRM.
function sendEmail() {
    var data = JSON.stringify({
        "IssueSend": "true"
    });
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            console.log("Email has been sent");
        }
    };

    xhr.open("POST", "<D365 Instance URL>/api/data/v9.1/emails(<GUID of email>)/Microsoft.Dynamics.CRM.SendEmail");
    xhr.setRequestHeader("Content-Type", " application/json");
    xhr.setRequestHeader("Prefer", " return=representation");
    xhr.send(data);
}

Leave a Reply

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