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

Day 11 - Customizing Option Sets Dropdown Fields with JavaScript

  In this Blog, we will see how to Customizing Option Sets Dropdown Fields with JavaScript label1 = formContext.getAttribute( "bosch_op...