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
  • Venkatesh Profile Picture
    Venkatesh 5 on at
    RE: Get Logged in/Current user's Business Unit

    Hi Bipin,

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

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Get Logged in/Current user's Business Unit

    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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans