web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Get Logged in/Current user's Business Unit

(0) ShareShare
ReportReport
Posted on by 50

Hi there,

I am writing a Javascript using XrmToolBox. It is a QuoteLine entity which every user/seller need to fill in the products, etc.

I'm trying to get the current user's business unit because every business unit has their own taxcode and I need to make specific calculations based on this.

So this is my current code:

function QuoteLineOnChange()
{
var taxcode = Xrm.Page.getAttribute("srkk_taxcode_");
var businessunit = Xrm.Page.getAttribute("owningbusinessunit").getValue()[0].name;


if (businessunit == "A1")
{
if (taxcode.getText() == "SST1")
{
Xrm.Page.getAttribute("srkk_tax").setValue(6);
Xrm.Page.getAttribute("priceperunit").fireOnChange();
}
else
{
Xrm.Page.getAttribute("srkk_tax").setValue(0);
Xrm.Page.getAttribute("priceperunit").fireOnChange();
}

}
else if (businessunit == "A2")
{
if (taxcodesg.getText() == "SR")
{
Xrm.Page.getAttribute("srkk_tax").setValue(7);
Xrm.Page.getAttribute("priceperunit").fireOnChange();
}
else
{
Xrm.Page.getAttribute("srkk_tax").setValue(7);
Xrm.Page.getAttribute("priceperunit").fireOnChange();
}

}
else
{
Xrm.Page.getAttribute("srkk_gstsst").setValue(0);
}
}

So as you can see, Business Unit A1 and A2 has their own value. But the seem cannot get the business unit that I refer to. Is there a problem on the RED code that I wrote? I need some help
I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Approach should be

    1. First get logged in user ID

    docs.microsoft.com/.../usersettings

    2. Use web api to fetch BU by passing the user ID

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/systemusers(26adf271-1a02-eb11-a812-000d3af01a07)?$select=_businessunitid_value", false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var _businessunitid_value = result["_businessunitid_value"];
                var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
                var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

    Replace User ID with result from first step.

    You code will not work because owningbusinessunit field is not present on form and it is not possible to put that field on form also.

    Please mark my answer verified if i were helpful

  • Venkatesh Profile Picture
    5 on at

    Hi Bipin,

    Could you share the code in javascript file which can be embedded as webresource . to get the business unit of user

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 128 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 113

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans