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