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.
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; } } } }
Thanks, I will dig into the material you posted via the hyperlink and see what I can do.
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,
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.
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; }
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156