Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

formContext.getServerURL() in D365 V9.1?

Posted on by 1,579

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. 

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: formContext.getServerURL() in D365 V9.1?

    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;
                }
            }
        }
    }

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: formContext.getServerURL() in D365 V9.1?

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

  • Verified answer
    erhan.keskin Profile Picture
    erhan.keskin 2,247 on at
    RE: formContext.getServerURL() in D365 V9.1?

    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,

  • Verified answer
    David Yu Profile Picture
    David Yu on at
    RE: formContext.getServerURL() in D365 V9.1?

    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

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: formContext.getServerURL() in D365 V9.1?

    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;
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans