Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Answered

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

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

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: This JS OnLoad script is getting email address in the API call, but not populating Form field

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

  • Verified answer
    Eiken Profile Picture
    Eiken on at
    RE: This JS OnLoad script is getting email address in the API call, but not populating Form field

    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
    a33ik 84,321 Most Valuable Professional on at
    RE: This JS OnLoad script is getting email address in the API call, but not populating Form field

    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?

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,570 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,683 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans