RE: Send email from case - populate to field with email address from a case field?
Hi dvn92,
The ‘To’ field in email is special field, it displays name in the field and store email address in database.
It consists following five types, so if you want to update ‘To’ field with the value you want, you need create a lookup field from these type instead of an address field on the case form.
For example, I new a lookup field(Accounts) named organisation on the case form, and use it to update ‘To’ field when I create an email from the case.
1.Js code:
function UpdateToField(executionContext) {
var formContext = executionContext.getFormContext();
var lookup = formContext.getAttribute("regardingobjectid").getValue(); // case lookup on email form
var newid = lookup[0].id.slice(1, -1);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v9.1/incidents(" newid ")?$select=_new_organisationid_value", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var _new_organisationid_value = result["_new_organisationid_value"];//id
var _new_organisationid_value_formatted = result["_new_organisationid_value@OData.Community.Display.V1.FormattedValue"];//name
var _new_organisationid_value_lookuplogicalname = result["_new_organisationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];//entity type
if (_new_organisationid_value != null) {
var value = new Array();
value[0] = new Object();
value[0].id = _new_organisationid_value;
value[0].name = _new_organisationid_value_formatted;
value[0].entityType = _new_organisationid_value_lookuplogicalname;
formContext.getAttribute("to").setValue(value); //set the lookup value finally
}
else
alert("organisation is null");
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
2.Test:
Regards,
Leah Ju
Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.