web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

JavaScript XRM library for model-driven/Dynamics Power app not populating field

(0) ShareShare
ReportReport
Posted on by 85

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.

I have the same question (0)
  • Kosenurm Profile Picture
    85 on at

    Full error is

    Web resource method does not exist:

    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)

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    remove the line lookupField.addOnChange(populateEmailAddress);

    and bind the populateEmailAddress to the onchange event of the lookup

    keep in mind that if the lookup points to aad user tab, the query to "systemusers" is not correct, should point to "aadusers", in that field the email address is inside the "userprincipalname" column

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 51 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans