Skip to main content
1-Visitor
April 19, 2021
Solved

Windchill REST API custom function return collection of custom type

  • April 19, 2021
  • 1 reply
  • 3357 views

How to modify the bellow example such that the custom function returns a collection of custom complexType records as JSON instead of only a single record?

 

rest/custom/domain/ToastsDomain.json

 

{
	"name": "ToastsDomain",
	"id": "ToastsDomain",
	"description": "ToastsDomain Domain",
	"nameSpace": "CUSTOM.ToastsDomain",
	"containerName": "Windchill",
	"defaultVersion": "1"
}

 

 

 

 

rest/custom/domain/ToastsDomain/v1/complexType/Toast.json

 

{
 "name": "Toast",
 "collectionName": "Toasts",
 "description": "No description.",
 "attributes": [
 {
 "name": "key",
 "type": "String"
 },
 {
 "name": "value",
 "type": "String"
 }
 ]
}

 

 

 

 

rest/custom/domain/ToastsDomain/v1/import.json

 

{
	"imports": [
		{"name": "PTC", "version": "3"}
	],
 "includeImportedEntitySets":false,
 "usePropertyPaths":true,
 "functions": [
 {
 "name": "GetToasts",
 "importName": "GetToasts",
 "description": "No description.",
 "includeInServiceDocument": false,
 "parameters": [
 {
 "name": "input",
 "type": "String",
 "isNullable": false
 }
 ],
 "returnType": {
 "type": "CUSTOM.ToastsDomain.Toast",
 "isCollection": false
 }
 }
 ]
}

 

 

 

 

rest/custom/domain/ToastsDomain/v1/import.js

 

function function_GetToasts(data, params) {
	var input = params.get('input').getValue();
	
	var ComplexValue = Java.type('org.apache.olingo.commons.api.data.ComplexValue');
	var complexValue = new ComplexValue();
	var Property = Java.type('org.apache.olingo.commons.api.data.Property');
	var ValueType = Java.type('org.apache.olingo.commons.api.data.ValueType');
	complexValue.getValue().add(new Property("String", "key", ValueType.PRIMITIVE, 'msg'));
	complexValue.getValue().add(new Property("String", "value", ValueType.PRIMITIVE, input));

	var EntityAttribute = Java.type('com.ptc.odata.core.entity.property.EntityAttribute');
	var PropertyValueType = Java.type('com.ptc.odata.core.entity.property.PropertyValueType');
	var entity = new EntityAttribute(null, 'Toast', PropertyValueType.COMPLEX, complexValue);
	return entity;
}

 

 

 

 

current output of GET http://<windchill_url>/Windchill/servlet/odata/v1/ToastsDomain/GetToasts(input='hello')

 

{
 "@odata.context": "http://<windchill_url>/Windchill/servlet/odata/v1/ToastsDomain/$metadata#CUSTOM.ToastsDomain.Toast",
 "@PTC.AppliedContainerContext.LocalTimeZone": "Europe/London",
 "key": "msg",
 "value": "hello"
}

 

 

 

 

 desired output example

 

{
 "@odata.context": "http://<windchill_url>/Windchill/servlet/odata/v1/ToastsDomain/$metadata#CUSTOM.ToastsDomain.Toast",
 "@PTC.AppliedContainerContext.LocalTimeZone": "Europe/London",
 "records": [
 {
 "key": "msg",
 "value": "hello"
 },
 {
 "key": "msg2",
 "value": "world"
 },
 ...
 ]
}

 

 

 

 

Best answer by James62

Bellow is the solution that outputs just a couple of records.

 

rest/custom/domain/ToastsDomain.json

{
	"name": "ToastsDomain",
	"id": "ToastsDomain",
	"description": "ToastsDomain Domain",
	"nameSpace": "CUSTOM.ToastsDomain",
	"containerName": "Windchill",
	"defaultVersion": "1"
}

 

 

rest/custom/domain/ToastsDomain/v1/complexType/Toast.json

{
 "name": "Toast",
 "collectionName": "Toasts",
 "description": "No description.",
 "attributes": [
 {
 "name": "key",
 "type": "String"
 },
 {
 "name": "value",
 "type": "String"
 }
 ]
}

 

 

rest/custom/domain/ToastsDomain/v1/import.json

{
	"imports": [
		{"name": "PTC", "version": "3"}
	],
 "includeImportedEntitySets":false,
 "usePropertyPaths":true,
 "functions": [
 {
 "name": "GetToasts",
 "importName": "GetToasts",
 "description": "No description.",
 "includeInServiceDocument": false,
 "parameters": [
 {
 "name": "input",
 "type": "String",
 "isNullable": false
 },
 {
 "name": "input2",
 "type": "String",
 "isNullable": false
 }
 ],
 "returnType": {
 "type": "CUSTOM.ToastsDomain.Toast",
 "isCollection": true
 }
 }
 ]
}

 

 

rest/custom/domain/ToastsDomain/v1/import.js

function function_GetToasts(data, params) {
	var input = params.get('input').getValue();
	var input2 = params.get('input2').getValue();

	var List = Java.type('java.util.ArrayList');
	var list = new List();
	
	var ComplexValue = Java.type('org.apache.olingo.commons.api.data.ComplexValue');
	var complexValue = new ComplexValue();
	var Property = Java.type('org.apache.olingo.commons.api.data.Property');
	var ValueType = Java.type('org.apache.olingo.commons.api.data.ValueType');
	complexValue.getValue().add(new Property("Edm.String", "key", ValueType.PRIMITIVE, 'msg'));
	complexValue.getValue().add(new Property("Edm.String", "value", ValueType.PRIMITIVE, input));
	list.add(complexValue);

	complexValue2 = new ComplexValue();
	complexValue2.getValue().add(new Property("Edm.String", "key", ValueType.PRIMITIVE, 'msg2'));
	complexValue2.getValue().add(new Property("Edm.String", "value", ValueType.PRIMITIVE, input2));
	list.add(complexValue2);
	
	var EntityAttribute = Java.type('com.ptc.odata.core.entity.property.EntityAttribute');
	var PropertyValueType = Java.type('com.ptc.odata.core.entity.property.PropertyValueType');
	var entities = new EntityAttribute(null, 'Toasts', PropertyValueType.COLLECTION_COMPLEX, list);
	return entities;
}

 

 

current output of GET http://<windchill_url>>/Windchill/servlet/odata/v1/ToastsDomain/GetToasts(input='hello',input2='world')

{
 "@odata.context": "http://<windchill_url>/Windchill/servlet/odata/v1/ToastsDomain/$metadata#Collection(CUSTOM.ToastsDomain.Toast)",
 "@PTC.AppliedContainerContext.LocalTimeZone": "Europe/London",
 "value": [
 {
 "key": "msg",
 "value": "hello"
 },
 {
 "key": "msg2",
 "value": "world"
 }
 ]
}

 

 

1 reply

James621-VisitorAuthorAnswer
1-Visitor
April 20, 2021

Bellow is the solution that outputs just a couple of records.

 

rest/custom/domain/ToastsDomain.json

{
	"name": "ToastsDomain",
	"id": "ToastsDomain",
	"description": "ToastsDomain Domain",
	"nameSpace": "CUSTOM.ToastsDomain",
	"containerName": "Windchill",
	"defaultVersion": "1"
}

 

 

rest/custom/domain/ToastsDomain/v1/complexType/Toast.json

{
 "name": "Toast",
 "collectionName": "Toasts",
 "description": "No description.",
 "attributes": [
 {
 "name": "key",
 "type": "String"
 },
 {
 "name": "value",
 "type": "String"
 }
 ]
}

 

 

rest/custom/domain/ToastsDomain/v1/import.json

{
	"imports": [
		{"name": "PTC", "version": "3"}
	],
 "includeImportedEntitySets":false,
 "usePropertyPaths":true,
 "functions": [
 {
 "name": "GetToasts",
 "importName": "GetToasts",
 "description": "No description.",
 "includeInServiceDocument": false,
 "parameters": [
 {
 "name": "input",
 "type": "String",
 "isNullable": false
 },
 {
 "name": "input2",
 "type": "String",
 "isNullable": false
 }
 ],
 "returnType": {
 "type": "CUSTOM.ToastsDomain.Toast",
 "isCollection": true
 }
 }
 ]
}

 

 

rest/custom/domain/ToastsDomain/v1/import.js

function function_GetToasts(data, params) {
	var input = params.get('input').getValue();
	var input2 = params.get('input2').getValue();

	var List = Java.type('java.util.ArrayList');
	var list = new List();
	
	var ComplexValue = Java.type('org.apache.olingo.commons.api.data.ComplexValue');
	var complexValue = new ComplexValue();
	var Property = Java.type('org.apache.olingo.commons.api.data.Property');
	var ValueType = Java.type('org.apache.olingo.commons.api.data.ValueType');
	complexValue.getValue().add(new Property("Edm.String", "key", ValueType.PRIMITIVE, 'msg'));
	complexValue.getValue().add(new Property("Edm.String", "value", ValueType.PRIMITIVE, input));
	list.add(complexValue);

	complexValue2 = new ComplexValue();
	complexValue2.getValue().add(new Property("Edm.String", "key", ValueType.PRIMITIVE, 'msg2'));
	complexValue2.getValue().add(new Property("Edm.String", "value", ValueType.PRIMITIVE, input2));
	list.add(complexValue2);
	
	var EntityAttribute = Java.type('com.ptc.odata.core.entity.property.EntityAttribute');
	var PropertyValueType = Java.type('com.ptc.odata.core.entity.property.PropertyValueType');
	var entities = new EntityAttribute(null, 'Toasts', PropertyValueType.COLLECTION_COMPLEX, list);
	return entities;
}

 

 

current output of GET http://<windchill_url>>/Windchill/servlet/odata/v1/ToastsDomain/GetToasts(input='hello',input2='world')

{
 "@odata.context": "http://<windchill_url>/Windchill/servlet/odata/v1/ToastsDomain/$metadata#Collection(CUSTOM.ToastsDomain.Toast)",
 "@PTC.AppliedContainerContext.LocalTimeZone": "Europe/London",
 "value": [
 {
 "key": "msg",
 "value": "hello"
 },
 {
 "key": "msg2",
 "value": "world"
 }
 ]
}