I'm trying to create an Email record and then send it using JS. Here is a portion of my code:
Xrm.WebApi.createRecord("email", data).then(
function success(result) {
var emailId = result.id;
emailId = emailId.substring(1,emailId.length-1);
window.parentFormContext.getAttribute("new_giresponseemail").setValue("/emails(" emailId ")");
var url = 'https://ORGNAME.crm.dynamics.com/api/data/v9.1/emails(' emailId ')/Microsoft.Dynamics.CRM.SendEmail';
var body = JSON.stringify({
"IssueSend": "true"
});
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.send(body);
Xrm.Utility.showProgressIndicator("Please wait a moment...");
req.onreadystatechange = function () {
Xrm.Utility.closeProgressIndicator();
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = "Response successfully sent!";
alert(result);
}
else if(this.status === 400){
var result = "ERROR: Please contact your CRM administrator.";
alert(result);
}
}
}
},
function (error) {
console.log(error.message);
}
)
This creates the email record correctly. However, when the XMLHTTPRequest is opened I'm getting a 400 response with the error message "')' or ',' expected at position 4 in (GUID)" for the API action. Sometimes the "position" number is different depending on the GUID that was generated for the new email record.
If I enter the API URL in the browser manually I get this error: "No HTTP resource was found that matches the request URI '">ORGNAME.crm.dynamics.com/.../Microsoft.Dynamics.CRM.SendEmail'."
I've scoured the internet trying to find a solution for this but no luck. Thanks for any help!