locked
Display Business Unit of Current logged in user on the Form Field RRS feed

  • Question

  • Hi,

    I am trying to fetch current logged in user's business unit on a form field and I am doing same with the following code

    function getRESTUrl()
    {
    return  Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc";

    //return https://polestarllp.api.crm8.dynamics.com/XRMServices/2011/Organization.svc
    }

    function loadUsersRequest()
    {
    var retrieveusersReq = new XMLHttpRequest();
    var curUserId = Xrm.Page.context.getUserId();
    retrieveusersReq.open("GET", getRESTUrl() +  "/SystemUserSet(guid'" + curUserId + "')", true);
    retrieveusersReq.setRequestHeader("Accept", "application/json");
    retrieveusersReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveusersReq.onreadystatechange = function ()
    {
    loadUsersResponse(retrieveusersReq);
    };
    retrieveusersReq.send();
    }

    function loadUsersResponse(retrieveusersReq)
    {
    if (retrieveusersReq.readyState == 4)
    {
    if (retrieveusersReq.status == 200)
    {
    var retrievedUsers = JSON.parse(retrieveusersReq.responseText).d.results;
    for (var i = 0; i < retrievedUsers.length; i++)
    {
    var user = retrievedUsers[i];
    alert(user.BusinessUnitId.Id);
    alert(user.BusinessUnitId.Name);
    }
    }
    else
    {
    errorHandler(retrieveusersReq);
    }
    }
    }

    function errorHandler(retrieveusersReq)
    {
    //show error message to end user
    }

    But it is not working properly and I added this JavaScript in Form Libraries with OnLoad event and I am getting error :

    There was an error with this field's customized event.

    Field: window

    Event: onload

    Error: undefined


    any help would be appreciated.

    Thanks,

    Dev

    Friday, April 7, 2017 2:40 PM

All replies

  • what crm version are you working on, if your on crm 2016 then getServerUrl no longer exists

    if on crm 2016 you need to use that version of the client api, getClientUrl

    crm 2016 client api reference

    Saturday, April 8, 2017 5:21 AM

  • Following are the issues I could see

    1. OData url has an issue - It should be /XRMServices/2011/Organizationdata.svc/SystemUserSet
    2. Based on the version of CRM the support for getserverurl varies. Latest versions does not support getServerUrl and uses getClientUrl.

    Suggestions,

    1. If you are using latest version of CRM I would recommend to go with webapi.
    2. If you are using older version, then go with the current approach and make use of ms generic library and msdn samples that will handle most of the logic when it comes to request and response. 
    3. The log file for this error should contain more details like line number of error, make use of it for effective development.

    All the best!


    Ramanathan Rajendran MCTS - Dynamics CRM

    Sunday, April 9, 2017 3:16 AM