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

Notifications

Announcements

No record found.

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,983 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans