cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
CCLam
Frequent Visitor

Javascript Async function to get user full name from systemuser table on model-driven app

Basically I want to get a set of user full names from the default "systemuser" table whenever user selects an option from a dropdown. Upon selecting an option, the name fields on the page will be updated with the user full names associated with the selected option.

 

To make it simple, for my example, there are 3 fields called Requester, 1stApprover, FinalApprover.

When user selects a dropdown, let's just call it approval flow, these 3 user name fields will be populated with the 3 user full names associated with the chosen approval flow.

 

Since I am new to javascript, I am still trying to understand the async/await/promise concept.

From the code below, I basically want to get the user full names from the retrievedResult (which I found that it is a Promise object, how do I make use of it?) to populate the 3 user name fields on the page. I am probably using the wrong syntax here, so could anyone point out the right way to achieve my desired outcome?

 

The Async function:

 

async function getUserFullName(userguid) {
    var results = await Xrm.WebApi.retrieveRecord("systemuser", userguid, "?$select=fullname").then(

        function success(resultuser) {
        },

        function (error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );

    return results;
} // end function getUserFullName

 

 

Portion of the main calling function from the dropdown on change:

 

...
for (var j = 0; j < result2.entities.length; j++) {
    retrievedResult = getUserFullName(result2.entities[j]._username_value);
    // use the results of getUserFullName to populate user name field
}
...

 

1 ACCEPTED SOLUTION

Accepted Solutions
a33ik
MVP

Hello,

 

It's more recommendation - for reusability purposes don't encapsulate one function into another.

And the answer why you got object instead of the fullname is because you did not reference the fullname property itself.

 The following code should give you what you need:

 

async function getUserFullName(guid) {
    var user = await Xrm.WebApi.retrieveRecord("systemuser", guid, "?$select=fullname");
    return user.fullname;
} // end function getUserFullName


async function formOnChange(executionContext) {

    userFullName = await getUserFullName("735b15d3-5c3b-ea11-a812-000d3a80c481");
    console.log("after call function: " + userFullName);
}

 

Also, as a recommendation, I would recommend checking the video on how to troubleshoot JavaScript for Dynamics/Dataverse - https://www.youtube.com/watch?v=DIAM4q28LXQ&ab_channel=AndrewButenko

 

View solution in original post

7 REPLIES 7
a33ik
MVP

Hello,

You're trying to mix up async/await and promises. If you want your code to work - don't mix it up. So it's either

 

async function whatever() {
var user = await Xrm.WebApi.retrieveRecord("systemuser", "Guid Here");
//using received user here
}

or

function someFunction(){
Xrm.WebApi.retrieveRecord("systemuser", "Guid Here").then(
function(result) {
//handling result here
}, function(e){
//handling error here
});
}

 

CCLam
Frequent Visitor

Thank you a33ik,

 

I have modified the async function as follows:

 

async function getUserFullName(guid) {
var user = await Xrm.WebApi.retrieveRecord("systemuser", guid);
return user.fullname;
}

 

 

In the main calling function:

 

...
for (var j = 0; j < result2.entities.length; j++) {
    userFullName = getUserFullName(result2.entities[j]._username_value);
    console.log("after call function: " + userFullName);
}
...

 

 

In the console, I'm getting:

after call function: [object Promise]

 

How should the code be corrected to display the retrieved fullname correctly?

Try this:

...
for (var j = 0; j < result2.entities.length; j++) {
    userFullName = await getUserFullName(result2.entities[j]._username_value);
    console.log("after call function: " + userFullName);
}
...

 

Also, mark the calling function as async as well.

CCLam
Frequent Visitor

I reduced the formOnChange function to just:

function formOnChange(executionContext) {

    async function getUserFullName(guid) {
        var user = await Xrm.WebApi.retrieveRecord("systemuser", guid, "?$select=fullname");
        return user;
    } // end function getUserFullName

    userFullName = getUserFullName("735b15d3-5c3b-ea11-a812-000d3a80c481");
    console.log("after call function: " + userFullName);
}

 console: after call function: [object Promise]

 

And if I add the async and await:

async function formOnChange(executionContext) {

    async function getUserFullName(guid) {
        var user = await Xrm.WebApi.retrieveRecord("systemuser", guid, "?$select=fullname");
        return user;
    } // end function getUserFullName

    userFullName = await getUserFullName("735b15d3-5c3b-ea11-a812-000d3a80c481");
    console.log("after call function: " + userFullName);
}

console: after call function: [object Object]

 

What am I missing?

a33ik
MVP

Hello,

 

It's more recommendation - for reusability purposes don't encapsulate one function into another.

And the answer why you got object instead of the fullname is because you did not reference the fullname property itself.

 The following code should give you what you need:

 

async function getUserFullName(guid) {
    var user = await Xrm.WebApi.retrieveRecord("systemuser", guid, "?$select=fullname");
    return user.fullname;
} // end function getUserFullName


async function formOnChange(executionContext) {

    userFullName = await getUserFullName("735b15d3-5c3b-ea11-a812-000d3a80c481");
    console.log("after call function: " + userFullName);
}

 

Also, as a recommendation, I would recommend checking the video on how to troubleshoot JavaScript for Dynamics/Dataverse - https://www.youtube.com/watch?v=DIAM4q28LXQ&ab_channel=AndrewButenko

 

CCLam
Frequent Visitor

Ah got it! Guess I was half awake.

 

And thanks for the recommendation. Not sure how I ended up encapsulating one function into another. The video is great as well! Was finding it a challenge to debug. Because I wasn't using the right tools and techniques! Thank you!

a33ik
MVP

Hello,
If my reply (or replies) answers your question take a minute and verify the answer.

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (2,585)