Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

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.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: JavaScript XRM library for model-driven/Dynamics Power app not populating field

    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

  • Kosenurm Profile Picture
    Kosenurm 85 on at
    RE: JavaScript XRM library for model-driven/Dynamics Power app not populating field

    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)

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans