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

This JS OnLoad script is getting email address in the API call, but not populating Form field

(0) ShareShare
ReportReport
Posted on by 85

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);
}

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Just curious - according to the code you use 9.1 - why don't you use Xrm.WebApi to query the data? Also Xrm.Page code is deprecated and recommended not to be used and use executionContext.getFormContext() to access the form's context.

    I haven't written the querying this way for a while so I can't guarantee that the code is 100% correct but it looks so from my memories. Do you get a valid email in the console?

  • Verified answer
    Eiken Profile Picture
    on at

    Hi,

    I recommend you to use Xrm.WebApi to get the information about user.

    This is my test code and populates the specified field on the Form successfully.

    function onFormLoad(e) {
        var formContext = e.getFormContext();
        // Get email
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
        var userGUID = userSettings.userId; 
        Xrm.WebApi.retrieveRecord("systemuser", userGUID, "?$select=internalemailaddress,fullname").then(
            function success(result) { 
                // Use your column name here 
                formContext.getAttribute("crd9d33_testemail").setValue(result.internalemailaddress);
            },
            function (error) {
            console.log(error.message);
            // handle error conditions
            }
            );
            // Get name
            var UserName = Xrm.Page.context.getUserName();   
            // Use your column name here 
           formContext.getAttribute("crd9d33_testname").setValue(UserName);
        }

    pastedimage1675826124547v1.png

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    If you got your issue sorted out, please, spend a minute closing the thread and verifying replies as answers that helped you.

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 49 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