Answered by:
jScript: use oData to fetch related records

Question
-
Hi there,
I am totally new with jQuery, OData and all that jazz, so please be gentle ;)
Here is the background:
- I created an entity called Industry that allow users to specify industry for accounts.
- The entity Industry has a relationship with itself, because an Industry can have "Sub Industries"
- In the Account, I added two lookups. One for Main Industry and another for Sub Industry. The Main Industry uses a specific view that only shows top level industries (that is, the ones without a parent specified), and the Sub Industry Lookup has a filter
so it only shows Industry records that has the Main Industry as their parent.
- I also used some jScript to disable the Sub Industry if the Main Industry is blank, and to clear the Sub industry whenever the Main Industry changes.
I am trying to make a solution for the Industry form that if the Sub Industry grid return any records (that is: if the Industry has any related industries), then it must disable the Parent Industry field. This is because I want to avoid users specifying the parent of a record that has child records (thus avoiding a 3 level, parent->child->grandchild relationship)
I understand that I must use jQuery and oData for this, so here is what I have done:
- I downloaded the last jQuery script (the compact, unreadable version) from the official website and added it as a web resource
- I added the jQuery script to my Industry form; but I did not specified a function for the onLoad event. I just added it as a form library
The I created a new jScript to be added to the onLoad() of my Industry form:
function checkSubgrid(parameter) { var AttributeName = Xrm.Page.getAttribute(parameter).getValue() ; var retrieveRecordsReq = new XMLHttpRequest(); var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/new_industrySet"; retrieveRecordsReq.open('GET', ODataPath, false); retrieveRecordsReq.setRequestHeader("Accept", "application/json"); retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveRecordsReq.send(null); var records = JSON.parse(retrieveRecordsReq.responseText).d; if(records.results.length > 0) { alert("Records present in Subgrid"); } }
I am not sure if my odata query is correct. But I can't actually test it because when I call the checkSubgrid function, I get the following error:
Field: windowEvent: onload
Error: 'JSON' is undefinedI am totally lost here... Any ideas of what I should do?
Thanks in advance for the help!
Cheers,
P.MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Tuesday, April 24, 2012 4:23 PM - I created an entity called Industry that allow users to specify industry for accounts.
Answers
-
Hi,
right now you have written query to fetch all of it's record, but I hope your requirement to check if current Industry has related (sub industry)?
so you need to chage your code like below
var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
var filter="/new_industrySet"?select=*&$filter=LookupFieldSchemaName/Id eq (guid'"+Xrm.Page.data.entity.getId()+"'); //change field name accordinglyretrieveRecordsReq.open('GET', ODataPath+filter, false);
HTH
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by pmdci Wednesday, April 25, 2012 9:27 AM
Tuesday, April 24, 2012 5:14 PMModerator
All replies
-
Hi,
change it to
var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Tuesday, April 24, 2012 4:34 PM
Tuesday, April 24, 2012 4:33 PMModerator -
Hi Mahender!
Thanks! The code works now. However thre is something wrong with this code since records.results.length is always being higher than 0 (it is returning the value 3).
Any ideas what might be wrong? Perhaps it is my oData query?
Cheers,
P.- Edited by pmdci Tuesday, April 24, 2012 4:43 PM
Tuesday, April 24, 2012 4:39 PM -
Hi,
right now you have written query to fetch all of it's record, but I hope your requirement to check if current Industry has related (sub industry)?
so you need to chage your code like below
var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
var filter="/new_industrySet"?select=*&$filter=LookupFieldSchemaName/Id eq (guid'"+Xrm.Page.data.entity.getId()+"'); //change field name accordinglyretrieveRecordsReq.open('GET', ODataPath+filter, false);
HTH
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by pmdci Wednesday, April 25, 2012 9:27 AM
Tuesday, April 24, 2012 5:14 PMModerator -
Hi Mahender,
It worked! here is my final code. I had to change the var filter line a little bit though:
function checkSubgrid(parameter) { var AttributeName = Xrm.Page.getAttribute(parameter).getValue() ; var retrieveRecordsReq = new XMLHttpRequest(); var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc"; var filter = "/new_industrySet?$filter=new_parentindustryid/Id eq guid'" + Xrm.Page.data.entity.getId() + "'"; retrieveRecordsReq.open('GET', ODataPath+filter, false); retrieveRecordsReq.setRequestHeader("Accept", "application/json"); retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveRecordsReq.send(null); var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d; if(records.results.length > 0) { alert(records.results.length + " record(s) present in Subgrid"); } }
However, if I try to create a new record, I get a JScript error saying:
Unable to get value of the property 'results': object is null or undefined
How do you suggest I resolve this error? I guess I could add a check for the form type at the beginning of the form to bypass the script?
- Edited by pmdci Wednesday, April 25, 2012 9:27 AM
Wednesday, April 25, 2012 9:16 AM -
Hi,
you should run your function only in update mode (because entity id will be present in update mode only) so you can apply below check
var FORM_TYPE_UPDATE = 2;
var formType = Xrm.Page.ui.getFormType();
if (formType == FORM_TYPE_UPDATE ) {//call your function here
}
you can refer : http://msdn.microsoft.com/en-us/library/gg327828.aspx#BKMK_getFormType
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Wednesday, April 25, 2012 9:37 AMModerator -
You are right :)
I might do it like this, though:
var FORM_TYPE_UPDATE = 2; var formType = Xrm.Page.ui.getFormType(); if (formType != FORM_TYPE_UPDATE ) { return; } //calling function here...
I tried calling it in the onSave event of a create form but I don't think it works very well. What I am going to do is create some messages around the form instructing the user. To re-open the record if he/she wants to add child records.
Thanks again!
P.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Wednesday, April 25, 2012 9:41 AM -
why you want to call it onSave ??, this code should be onload.
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Wednesday, April 25, 2012 9:48 AMModerator -
I wanted to call it onSave as well because if a record was saved, then I recon that the could would reload and it would work ok.
However the onSave call happens before the record is saved, not after. so that doesn't work.
But this is great stuff. Thank you for your help. I couldn't have done it without you, and now I understand oData a bit better! :)
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Wednesday, April 25, 2012 9:55 AM -
Mahender,
Just one thing. I removed the jQuery library from this solution and I've been running it without any issues.
I thought that oData required jQuery for it to work?
Cheers,
P.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Wednesday, April 25, 2012 12:32 PM