
I'm creating a JS function which does some things depending on whether the target entity "Collection" is associated to active records of another entity "Library". The entities are associated by an N:N relationship.
I wrote the following fetchXML, but I'm not sure if it's the right approach to query a N:N relationship, can you help me to check it please?
// I look for "stores"...
// ...which are associated to "collections"
// ("from" indicates the entity I'm querying for,
// "to" indicates the attribute of the other entity in the relation)
...
I stored the fetchXML query inside a variable which I'm gonna use to filter the result and set the conditions:
function myFunction(executionContext) {
var formContext = executionContext.getFormContext();
// I store the value for the id of this collection:
var collectionid = executionContext.getAttribute("collectionid").getValue();
// I sotre the value if the FetchXML inside a variable:
var resultStores = "
"
foreach (s in resultStores){
// some actions here
}
else{
// show alert
}
} Hi Joel,
I am not very clear with the question however I do see some issues :
1. The prefix 'ava' is missing in most places.
2. the comparison with collectionid is not clear - if it is just a single value you can write the variable name directly. If it is multiple values then you need to use 'In' and not 'eq'.
3. statecode is not available on the N:N relationship. If it is on the store entity, you need to add a new filter. If it on collection then the new link entity needs to be added.
I believe code should be more like this :
function myFunction(executionContext) {
var formContext = executionContext.getFormContext();
// I store the value for the id of this collection:
var collectionid = executionContext.getAttribute("ava_ecncylopediaid").getValue().replace(/[{}]/g, '');
// I sotre the value if the FetchXML inside a variable:
var resultStores = " "
""
""
""
""
""
""
""
""
""
""
""
""
""
"";
var serverURL = formContext.context.getClientUrl();
var fetch = encodeURI(resultStores);
var entityname = "ava_store";
var Query = entityname "?fetchXml=" fetch;
var req = new XMLHttpRequest();
req.open("GET", serverURL "/api/data/v9.1/" Query, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
//foreach
}
}
}
req.send();
}