Hi,
I have this code which OnLoad of main Form in model-driven app is populating a name field with the user's name, and I want another field populated with their email address. In browser Dev tools, the API call is correctly returning internalemailaddress, but it is not populating the specified field on the Form. What am I doing wrong?
function onFormLoad() {
var userId = Xrm.Page.context.getUserId();
var request = new XMLHttpRequest();
var newuserId = userId.replace(/{|}/g, "");
request.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/systemusers(" + newuserId + ")?$select=internalemailaddress", true);
request.setRequestHeader("OData-MaxVersion", "4.0");
request.setRequestHeader("OData-Version", "4.0");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
request.onreadystatechange = function() {
if (this.readyState === 4) {
request.onreadystatechange = null;
if (this.status === 200) {
var user = JSON.parse(this.response).value;
var emailAddress = user[0].internalemailaddress;
console.log(emailAddress);
Xrm.Page.getAttribute("crc1e_csosubmitteremail").setValue(emailAddress);
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
request.send();
var userName = Xrm.Page.context.getUserName();
Xrm.Page.getAttribute("new_csosubmittername").setValue(userName);
}