Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

above to retrieve the value in Javascript but unable to return it

Posted on by 725

Hi,

I have code below to retrieve business unit of current user, the 1st console log console.log("BU from result: " + businessUnitName) shows the proper business unit name, but console.log("BU from GetBusinessUnit: " + businessUnitName) is showing as undefined, any advice is appreciated.

function GetBusinessUnit() {

var roleId = Xrm.Page.context.getUserId();
var businessUnitName;
Xrm.WebApi.online.retrieveRecord("systemuser", roleId, "?$select=_businessunitid_value").then(
function success(result) {
console.log(result);
businessUnitName = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
console.log("BU from result: " + businessUnitName);
},
function(error) {
console.log(error.message);
}
);
console.log("BU from GetBusinessUnit: " + businessUnitName);
return businessUnitName;
}

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: above to retrieve the value in Javascript but unable to return it

    thank you, I used this and rewrite all functions of that js file to async function, it is calling the value correctly now.

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: above to retrieve the value in Javascript but unable to return it

    Guido Preite  Thank you for the reply and thank you for your Dataverse REST Builder, for some reason CRM REST Builder was able to read most entities I tried but stuck at loading when it try to retrieve fields from systemuser entity, and the Github repository for this tool does not seem to be accessible anymore.  Your tool was able to pull the fields that I needed.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: above to retrieve the value in Javascript but unable to return it

    Hi Tyler S,

    This is because Xrm.WebApi.online.retrieveRecord() is a async function. When you console.log 'businessUnitName', this function hasn't executed successfully. So you would get undefined. Please change you code like this:

    async function GetBusinessUnit() {
    	var roleId = Xrm.Page.context.getUserId();
    	var businessUnitName;
    	var result = await Xrm.WebApi.online.retrieveRecord("systemuser", roleId, "?$select=_businessunitid_value");
    	console.log("BU from GetBusinessUnit: "   result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"]);
    	return result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
    }

    Since it returns a promise, so you could directly add async at function and await Xrm.WebApi.online.retrieveRecord() executed successfully is ok.

    Reference:

    docs.microsoft.com/.../retrieverecord

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: above to retrieve the value in Javascript but unable to return it

    because the Xrm.WebApi is async, meaning that the lines

    console.log("BU from GetBusinessUnit: " + businessUnitName);

    are executed before the call is completed, you will need to rewrite your logic to handle an async pattern

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans