Skip to content
Advertisement

How to get current user’s business unit in Dynamics 365

How can I get business unit details of the logged-in user with javascript? I tried Xrm.Utility.getGlobalContext().userSettings but I couldn’t get any information for business unit

Advertisement

Answer

If you write Xrm.Utility.getGlobalContext().userSettings I assume you are working with client-side javascript inside a Model-driven app.

From the userSettings you can get the userId property, it returns the GUID of the current user.

After you have this value in order to get details from the business unit of the user you need to do a retrieve request, something like this:

// get the userId
var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
// remove { and } from the userId
userId = userId.replace("{", "").replace("}", "");
// Xrm.WebApi call to retrieve details of the user (fullname)
// and the name of the businessunit (name from expand)
Xrm.WebApi.online.retrieveRecord("systemuser", 
userId,
"?$select=fullname&$expand=businessunitid($select=name)").then(
function success(result) {
    console.log(result);
    // Columns
    var systemuserid = result["systemuserid"]; // Guid
    var fullname = result["fullname"]; // Text
    
    // Many To One Relationships
    if (result.hasOwnProperty("businessunitid")) {
        var businessunitid_name = result["businessunitid"]["name"]; // Text
    }
},
function(error) {
    console.log(error.message);
}
);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement