Answered by:
odata query in a web resource

Question
-
HI,
Can I use multiple odata queries in multiple functions like this?
function getUserCounty(userId) { debugger; var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + userId + "')", false); 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 county = retrievedUser.new_County; // new_County must be the lookup field name inside user entity return county; } else { return "error"; } } var owner = Xrm.Page.getAttribute("ownerid").getValue(); if(owner!=null) { var ownerId = owner[0].id; var county = getUserCounty(ownerId); var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; countyValue[0].name = county.Name; countyValue[0].entityType = county.LogicalName; Xrm.Page.getAttribute("new_county").setValue(countyValue); if(county!=null) { var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; var countyId=countyValue[0].id; } var countyNumber1=FillCountyNumber(countyId); Xrm.Page.getAttribute("new_CountyNumber").setValue(countyNumber1); } function FillCountyNumber(countyId){ var server2Url = Xrm.Page.context.getClientUrl(); var ODataPath2 = server2Url + "/XRMServices/2011/OrganizationData.svc"; var user2Request = new XMLHttpRequest(); user2Request.open("GET", ODataPath2 + "/new_countySet?$select=new_CountyNumber&$filter=new_countyId/Id eq '"+countyId+"'", false); user2Request.setRequestHeader("Accept", "application/json"); user2Request.setRequestHeader("Content-Type", "application/json; charset=utf-8"); user2Request.send(); if (user2Request.status === 200) { var retrievedUser = JSON.parse(user2Request.responseText).d; var countyNumber = retrievedUser.new_CountyNumber; return countyNumber; } else { return "error"; } }
I see the status as 400 and throwing error. Thanks
Tuesday, February 10, 2015 10:00 PM
Answers
-
I figured it out. I forgot to include guid in my 2nd odata query.
userRequest.open("GET", ODataPath + "/new_countySet?$select=new_CountyNumber&$filter=new_countyId eq (guid'"+countyId+"')", false);
I've passed through this error but surprisingly,
var countyNumber = retrievedUser.results[0].new_CountyNumber;
in the above line retrievedUser shows new_CountyNumber value when debug was done but countyNumber variable is returning undefined value. What could be the reason?
Thanks
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, February 11, 2015 6:17 PM
- Edited by ReignFan Wednesday, February 11, 2015 6:37 PM Made a small change in assigning the value to countyNumber.
Wednesday, February 11, 2015 3:41 PM
All replies
-
Hi,
I am working on a javascript to autopopulate two attributes on a form onload. One attribute should show up based on the other attribute. Both the attributes are populated through javascript. I did debug the script and all the attributes are returning values as they should except the odata query returning 400 error. I don't understand what is wrong with the query as I built it with odata query designer tool and the url returns data in xml too. Here is my code:
function getUserCounty(userId) { debugger; var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + userId + "')", false); 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 county = retrievedUser.new_County; // new_County must be the lookup field name inside user entity return county; } else { return "error"; } } var owner = Xrm.Page.getAttribute("ownerid").getValue(); if(owner!=null) { var ownerId = owner[0].id; var county = getUserCounty(ownerId); var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; countyValue[0].name = county.Name; countyValue[0].entityType = county.LogicalName; Xrm.Page.getAttribute("new_county").setValue(countyValue); if(county!=null) { var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; var countyId=countyValue[0].id; } var countyNumber1=FillCountyNumber(countyId); Xrm.Page.getAttribute("new_CountyNumber").setValue(countyNumber1); } function FillCountyNumber(countyId){ var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/new_countySet?$select=new_CountyNumber&$filter=new_countyId eq '"+countyId+"'", false); 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 countyNumber = retrievedUser.new_CountyNumber; return countyNumber; } else { return "error"; } }
Thanks for any help.
- Merged by Andrii ButenkoMVP, Moderator Wednesday, February 11, 2015 2:51 AM The same thread
Thursday, February 5, 2015 7:17 PM -
-
I think your issue is the structure of the filter clause. It looks like new_countyId is an EntityReference; to filter these you need syntax like:
new_countyId/Id eq (guid'189C0AD2-00ED-4E1E-AF9D-59531DB0E66D')
where 189C0AD2-00ED-4E1E-AF9D-59531DB0E66D is an example Guid
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Friday, February 6, 2015 11:30 AMModerator -
The url is returning data that I want. My url looks like this:
https://xxxxxxxxxxx.com/xrmservices/2011/OrganizationData.svc/new_countySet?$select=new_CountyNumber&$filter=new_countyId eq guid'125G0FR4-1995-e411-9331-0052569c456b'
@David - new_countyId is the primary key of countySet and since the url is returning the data, I think url part is correct. But in this line:
if (userRequest.status === 200) {
status is showing 400 which is failing and returning "error". I don't understand what is the reason!
Thanks
Friday, February 6, 2015 1:31 PM -
If you just want a record by the primary key, you don't need to use a filter clause. Instead you can use the retrieve syntax:
https://xxxxxxxxxxx.com/xrmservices/2011/OrganizationData.svc/new_countySet(guid'125G0FR4-1995-e411-9331-0052569c456b')?$select=new_CountyNumber
The other thing to double-check is the case of the entity and attribute names
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Friday, February 6, 2015 3:12 PMModerator -
Use the code below:
function getUserCounty(userId) { debugger; var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + userId + "')", false); userRequest.setRequestHeader("Accept", "application/json"); userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); userRequest.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 200) { var retrievedUser = JSON.parse(userRequest.responseText).d; var county = retrievedUser.new_County; // new_County must be the lookup field name inside user entity return county; } } }; userRequest.send(); // if (userRequest.status === 200) { // var retrievedUser = JSON.parse(userRequest.responseText).d; // var county = retrievedUser.new_County; // new_County must be the lookup field name inside user entity // return county; // } // else { // return "error"; // } } var owner = Xrm.Page.getAttribute("ownerid").getValue(); if(owner!=null) { var ownerId = owner[0].id; var county = getUserCounty(ownerId); var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; countyValue[0].name = county.Name; countyValue[0].entityType = county.LogicalName; Xrm.Page.getAttribute("new_county").setValue(countyValue); if(county!=null) { var countyValue = new Array(); countyValue[0] = new Object(); countyValue[0].id = county.Id; var countyId=countyValue[0].id; } var countyNumber1=FillCountyNumber(countyId); Xrm.Page.getAttribute("new_CountyNumber").setValue(countyNumber1); } function FillCountyNumber(countyId){ var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var userRequest = new XMLHttpRequest(); userRequest.open("GET", ODataPath + "/new_countySet?$select=new_CountyNumber&$filter=new_countyId eq '"+countyId+"'", false); userRequest.setRequestHeader("Accept", "application/json"); userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); userRequest.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 200) { var retrievedUser = JSON.parse(userRequest.responseText).d; var countyNumber = retrievedUser.new_CountyNumber; return countyNumber; } } }; userRequest.send(); // userRequest.send(); // if (userRequest.status === 200) { // var retrievedUser = JSON.parse(userRequest.responseText).d; // var countyNumber = retrievedUser.new_CountyNumber; // return countyNumber; // } // else { // return "error"; // } }
Regards, Saad
Wednesday, February 11, 2015 6:07 AM -
Thanks Saad. But the status is still showing 400 instead of 200 and not returning CountyNumber.Wednesday, February 11, 2015 1:54 PM
-
I figured it out. I forgot to include guid in my 2nd odata query.
userRequest.open("GET", ODataPath + "/new_countySet?$select=new_CountyNumber&$filter=new_countyId eq (guid'"+countyId+"')", false);
I've passed through this error but surprisingly,
var countyNumber = retrievedUser.results[0].new_CountyNumber;
in the above line retrievedUser shows new_CountyNumber value when debug was done but countyNumber variable is returning undefined value. What could be the reason?
Thanks
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, February 11, 2015 6:17 PM
- Edited by ReignFan Wednesday, February 11, 2015 6:37 PM Made a small change in assigning the value to countyNumber.
Wednesday, February 11, 2015 3:41 PM