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.