Hi,
I am trying to refactor my script in order to use web api. We are about to upgrade to CRM 2016. This particular script has worked fine until now.
But in CRM 2016, it is failing to work and upon some research I found it might be due to use of old odata endpoints in the script. So I tried to use the new web api url in the script but it still fails.
I did debug the script and found my userRequest.status gives me 0 for some reason. I've also used plural name of the entity in query.
Can some one please guide me in the right direction? Thanks for any help.
All this script does is, takes user id and retrieves an attribute(country) from the systemuser entity filtered on the user id.
function getUserCountry(userId) { debugger; var createdOn=Xrm.Page.getAttribute("createdon").getValue(); if(createdOn!=null) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); //var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var ODataPath = serverUrl + "/api/data/v8.0"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/SystemUsersSet(guid'" + userId + "')?$select=new_country", true); userRequest.setRequestHeader("OData-MaxVersion", "4.0"); userRequest.setRequestHeader("OData-Version", "4.0"); userRequest.setRequestHeader("Accept", "application/json"); userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); userRequest.send(); if (userRequest.status === 200) { var retrievedUser = JSON.parse(userRequest.responseText).d; var country = retrievedUser.new_country; // new_country is the lookup field name inside user entity return country; } else { return "error"; } } var owner = Xrm.Page.getAttribute("ownerid").getValue(); if(owner!=null) { var ownerId = owner[0].id; var country = getUserCountry(ownerId); if(country!=null) { var countryValue = new Array(); countryValue[0] = new Object(); countryValue[0].id = country.Id; countryValue[0].name = country.Name; countryValue[0].entityType = country.LogicalName; Xrm.Page.getAttribute("new_country").setValue(countryValue); }