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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

formContext.getServerURL() in D365 V9.1?

(0) ShareShare
ReportReport
Posted on by 1,589

I am trying to use JavaScript in a D365 V9.x Cloud Environment to get the business unit for a specific user based on the specific user’s GUID.

The code shown below bombs out on the following line:

var odataSelect = formContext.getServerUrl()   "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId&$filter=SystemUserId eq guid'"   loggedInUserID   "'";
  

The error it give is as follows:

One of the scripts for this record has caused an error. For more details, download the log file.
TypeError: formContext.getServerUrl is not a function at FillBU (https://cppdev.crm.dynamics.com/{637182541550013230}/WebResources/cpp_/entities/datatransfer/datatransfer.js?ver=1827875685:72:35)

 

TypeError: formContext.getServerUrl is not a function

    at FillBU (https://cppdev.crm.dynamics.com/{637182541550013230}/WebResources/cpp_/entities/datatransfer/datatransfer.js?ver=1827875685:72:35)

    at OnLoad (https://cppdev.crm.dynamics.com/{637182541550013230}/WebResources/cpp_/entities/datatransfer/datatransfer.js?ver=1827875685:21:13)

    at eval (eval at RunHandlerInternal (https://cppdev.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=1827875685:154:1), :1:1)

    at RunHandlerInternal (https://cppdev.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=1827875685:160:1)

    at RunHandlers (https://cppdev.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=1827875685:118:1)

    at ExecuteHandler (https://cppdev.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=1827875685:81:1)

    at Mscrm.TurboForm.Control.CustomScriptsManager.$Dq_1 (https://cppdev.crm.dynamics.com/_static/form/formcontrols.js?ver=1827875685:5177:100)

    at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (https://cppdev.crm.dynamics.com/_static/form/formcontrols.js?ver=1827875685:5093:18)

    at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (https://cppdev.crm.dynamics.com/_static/form/formcontrols.js?ver=1827875685:5125:18)

    at https://cppdev.crm.dynamics.com/_static/form/formcontrols.js?ver=1827875685:5157:19

The entire function (referenced as ‘code shown below’ from the first line)  is shown below so you can see the greater context of the line that bombs out. 

function FillBU(executionContext, loggedInUserID) {
    var formContext = executionContext.getFormContext();
    var odataSelect = formContext.getServerUrl()   "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId&$filter=SystemUserId eq guid'"   loggedInUserID   "'";
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", odataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        getFieldData(this);
    };
    retrieveReq.send();
 
    function getFieldData(retrieveReq) {
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
                var retrievedValue = retrieved.results[0].BusinessUnitId;
                var id = retrievedValue.Id;
                var name = retrievedValue.Name;
                alert(id   "   "   name);
                //return id;
            }
        }
    }

I have searched for a V9.x version of Xrm.Page.Context.getServerURL() but have come up empty to I tried formContext.getServerURL() and clearly as you can see here, that does not work. 

I also tried using formContext.getClientURL but encounter the same error message verbatim excerpt the error is TypeError: formContext.getClientURL is not a function instead of saying getServerURL is not a function, 

Any help or guidance would be greatly appreciated. 

I have the same question (0)
  • ACECORP Profile Picture
    1,589 on at

    UPDATE —  businessunitid is not a property of userSettings so what I was hoping to be able to do does not work. 

    DOES NOT WORK — I believe I have a solution. See below:

    function GetUserBU(executionContext) {
        var formContext = executionContext.getFormContext();
        var userSettings = Xrm.Utility.getGlobalContext().userSettings
        var UserId = userSettings.userId;
        var UserName = userSettings.userName;
        var UserBU = userSettings.BusinessUnitId;
        return UserBU;
    }

  • Verified answer
    David Yu Profile Picture
    Microsoft Employee on at

    Hey Jim,

    You may use the following JS code to get URL information.

    var globalContext = Xrm.Utility.getGlobalContext();

    globalContext.getClientUrl();

    For more information you may check the following document.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utility/getglobalcontext/getclienturl

  • Verified answer
    erhan.keskin Profile Picture
    2,253 on at

    Hi,

    You can use this to get the URL.

    //Get the form context

    var formContext = executionContext.getFormContext();

    //Get the Client Url

    var clientUrl = formContext.context.getClientUrl();

    Regards,

  • ACECORP Profile Picture
    1,589 on at

    Thanks, I will dig into the material you posted via the hyperlink and see what I can do.

  • ACECORP Profile Picture
    1,589 on at

    Here is the final result — which does in-fact work! Thanks for all your help!

    function GetUserBusinessUnit(executionContext, loggedInUserID) {
        var formContext = executionContext.getFormContext();
        var clientUrl = formContext.context.getClientUrl();
        var odataSelect = clientUrl   "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId&$filter=SystemUserId eq guid'"   loggedInUserID   "'";
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", odataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            getFieldData(this);
        };
        retrieveReq.send();
     
        function getFieldData(retrieveReq) {
            if (retrieveReq.readyState == 4) {
                if (retrieveReq.status == 200) {
                    var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
                    var retrievedValue = retrieved.results[0].BusinessUnitId;
                    var id = retrievedValue.Id;
                    var name = retrievedValue.Name;
                    alert(id   "   "   name);
                    //return id;
                }
            }
        }
    }

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Subra Profile Picture

Subra 107

#2
11manish Profile Picture

11manish 91

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 85 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans