Tuesday, March 15, 2016

Get Business Unit Id For the User CRM Javascript

Hi All,

JavaScript  to get Business Unit ID For the User

var businessUnitId = GetBusinessUnitIDFortheUser (loggedInUser);

function GetBusinessUnitIDFortheUser(userId) {
    try {
        if (serverUrl == null) {
            serverUrl = GetCRMServerUrl();
        }

        var oDataEndpointUrl = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/";
        oDataEndpointUrl += "SystemUserSet?$select=BusinessUnitId&$filter=SystemUserId eq guid'" + userId + "'";
        var service = GetRequestObject();

        if (service != null) {
            service.open("GET", oDataEndpointUrl, false);
            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
            service.setRequestHeader("Accept", "application/json,text/javascript, */*");
            service.send(null);

            var requestResults = eval('(' + service.responseText + ')').d;
            if (requestResults != null && requestResults.results.length == 1) {
                var userRecord = requestResults.results[0];
                if (userRecord.BusinessUnitId && userRecord.BusinessUnitId.Id) {
                    return userRecord.BusinessUnitId.Id;
                }
            }
        }

        return "";
    }
    catch (err) {
        return "";
    }
}


1 comment:

  1. I think that thanks for the valuabe information and insights you have so provided here. dig this

    ReplyDelete

How to Clear Cache in Canvas PowerApps while working on Offline mode?

  Introduction In this blog, we’ll look at how to clear cache in Canvas Apps when using the Power Apps mobile application, especially when t...