Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Hello everyone!
I get information from the server using the OData REST API.
I have a global attribute, which is a collection:
I want the server to return only one value from the collection when requested. In the request, I want to specify a value that will determine the desired collection value.
I understand that such logic can not be written in the request. Therefore, I tried to implement this in the JavaScript domain file.
I thought that it should be a function that first receives the values of the collection attributes from Windchill, matches them, and then uses the value passed to the function as a key and returns the desired value.
I saw that in JavaScript you can use classes from Java:
var EntityAttribute = Java.type('com.ptc.odata.core.entity.property.EntityAttribute');
But I do not know how to use the code to get the values of global attributes.
I did not find documentation for odataWeb.jar (com.ptc.odata...) library.
Tell me, please, how can I do this?
Thank you in advance!
Best Regards!
Hi VladiSlav,
Are you trying to search for parts by the value of this collection?
Have you tried the filter?
/Windchill/servlet/odata/ProdMgmt/Parts?$filter=contains(ThisAttr,'36')
Keep in mind that the response might be in XML or JSON format, to request it as a JSON just include it in your request header.
Here is a JS example of an async function that looks for a part by number:
function getWcPartByNumber(number, callback) {
let filter = "$filter=Number eq '" + number + "'";
let orderBy = "$orderby=Revision desc";
let filterUrl = wcHost + "/Windchill/servlet/odata/ProdMgmt/Parts?" + filter + "&" + orderBy;
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status != 200) {
callback(this.statusText, this);
}
if (this.readyState == 4 && this.status == 200) {
callback(null, this);
}
};
xhttp.open("GET", filterUrl, true);
xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhttp.setRequestHeader("dataType", "json");
xhttp.setRequestHeader("Prefer", "odata.maxpagesize=200");
xhttp.send(null);
}
And here is a call for this function with the following parsing of the response and a check if this collection of yours contains a certain value:
getWcPartByNumber (wtPartNumber, function(err, data) {
if (err != null) {
alert (err);
} else {
let dataObj = JSON.parse(data.responseText);
if (dataObj.value.length != 0 && dataObj.value[0].ThisAttr.includes(36)) {
// Do Something
}
}
});
With XML response it should be somewhat similar.
Kind regards,
Dmitry.
Hi VladiSlav,
Are you trying to search for parts by the value of this collection?
Have you tried the filter?
/Windchill/servlet/odata/ProdMgmt/Parts?$filter=contains(ThisAttr,'36')
Keep in mind that the response might be in XML or JSON format, to request it as a JSON just include it in your request header.
Here is a JS example of an async function that looks for a part by number:
function getWcPartByNumber(number, callback) {
let filter = "$filter=Number eq '" + number + "'";
let orderBy = "$orderby=Revision desc";
let filterUrl = wcHost + "/Windchill/servlet/odata/ProdMgmt/Parts?" + filter + "&" + orderBy;
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status != 200) {
callback(this.statusText, this);
}
if (this.readyState == 4 && this.status == 200) {
callback(null, this);
}
};
xhttp.open("GET", filterUrl, true);
xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhttp.setRequestHeader("dataType", "json");
xhttp.setRequestHeader("Prefer", "odata.maxpagesize=200");
xhttp.send(null);
}
And here is a call for this function with the following parsing of the response and a check if this collection of yours contains a certain value:
getWcPartByNumber (wtPartNumber, function(err, data) {
if (err != null) {
alert (err);
} else {
let dataObj = JSON.parse(data.responseText);
if (dataObj.value.length != 0 && dataObj.value[0].ThisAttr.includes(36)) {
// Do Something
}
}
});
With XML response it should be somewhat similar.
Kind regards,
Dmitry.
Thank you very much for your reply!
Your answer tells me that I can pass any value to a function
But, unfortunately, I need to get not part, but the value of IBA.
Tell me, please, how can I run such a function that does not have the prefix "function_"?