I have built a custom PowerApps model-driven app which is in very wide use across our organisation which manages workflow and database administration for our contracts.
In this app, the main Form is virtually all free-text input fields plus some Choice columns. What we would now like to do is, for some fields where the user is required to enter a Colleague’s name, is lookup to the AAD User table. This is a pretty straightforward Lookup column. However, what we then want is, when the person has been ‘lookedup’ and selected in this field, is to populate another column field automatically (so not on saving the record) with the selected Colleague’s email address which is in the AAD User table. I’m hoping I can get the JS code nailed for this, which I can then reuse across other fields where similar will likely be required.
I have written this code and added it as a web resource and associated it to the Form...
function populateEmailAddress() { var lookupField = Xrm.Page.getAttribute("crc1e_peoplepickertest"); var emailAddressField = Xrm.Page.getAttribute("crc1e_ppemail");if (lookupField.getValue() != null) {
var lookupValue = lookupField.getValue()[0];
var lookupId = lookupValue.id;
var lookupType = lookupValue.entityType;
if (lookupType === "systemuser") {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/systemusers(" + lookupId + ")?$select=internalemailaddress", false);
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);
emailAddressField.setValue(result["internalemailaddress"]);
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}
}
lookupField.addOnChange(populateEmailAddress);
I think I also need an event handler function programmed into the OnChange property of the lookup field, so I added 'lookupField.addOnChange(populateEmailAddress);' to this.
When I now select someone in the lookup field, I get this error...
lookupField.addOnChange(populateEmailAddress); Session Id: 152577a4-e6ac-4e3a-9ef9-a9478a200576 Correlation Id: 3d86a64e-61d9-4a57-b98d-0ee27c580c58 Event Name: onchange Function Name: lookupField.addOnChange(populateEmailAddress); Web Resource Name: crc1e_LookupJava Solution Name: Active Publisher Name: Redacted Time: Mon Feb 06 2023 13:20:51 GMT+0000 (Greenwich Mean Time)
What am I doing wrong?
Thanks
K.