cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Custom OData Action returns JSON data as a string value

alexisraj
4-Participant

Custom OData Action returns JSON data as a string value

I have this action configured:

      {

         "name":"InitiateApprovalProcess",

         "description":"Initiate the approval process",

         "parameters":[

            {

               "name":"ChangeNotice",

               "type":"ChangeNotice",

               "isNullable":false,

               "isCollection":false

            },

            {

               "name":"CreationDate",

               "type":"String",

               "isNullable":false

            }

         ],

         "returnType":{

            "type":"String",

            "isCollection":false

         }

      }

 

With this JS:

function action_InitiateApprovalProcess(data, params) {

     var ChangeNoticeProcessor = Java.type("com.xyz.smb.processors.ChangeNoticeProcessor");

    return ChangeNoticeProcessor.initiateApprovalProcess(data, params);

}

 

And this Java Code (I have removed the product details etc.):

public static ActionResult initiateApprovalProcess(ActionProcessorData data, Map<?, ?> parameters) {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("SID", "SID");

        jsonObject.put("RID", "RID");

        jsonObject.put("DATE", "1.1.2020");

 

        JSONArray rpArray = new JSONArray();

        JSONObject rpObject = new JSONObject();

        rpObject.put("CLS", "VLASS1232");

        rpObject.put("POS", "PS11");

        rpObject.put("NDE", "ND12");

        rpObject.put("GD", "GUID1234");

        rpObject.put("DESC", "DESC045");

        rpObject.put("MT", "MAT345");

        rpObject.put("QTY", "QTY034");

        rpObject.put("UNIT", "UNOT12");

        rpObject.put("BOOL", "TRUE");

        rpObject.put("DR", "DRR001");

        rpObject.put("DRLT", "OO");

        rpArray.add(0, rpObject);

        jsonObject.put("DTA", rpArray);

 

        EntityAttribute retEntity = new EntityAttribute("Edm.String", null, PropertyValueType.PRIMITIVE,  jsonObject.toString());

        ActionResult actionResult = new ActionResult();

        actionResult.setReturnedObject(retEntity);

}

 

We receive the JSON output/response as a string value. We do not receive it as we would expect a JSON data format.

Expected result:

JSON response:

JSON response.png

Actual result:

Response:

"{\"SID\":\"SID\",\"RID\":\"RID\",\"DATE\":\"1.1.2020\",\"DTA\":[{\"CLS\":\"VLASS1232\",\"POS\":\"PS11\",\"NDE\":\"ND12\",\"GD\":\"GUID1234\",\"DESC\":\"DESC045\",\"MT\":\"MAT345\",\"QTY\":\"QTY034\",\"UNIT\":\"UNOT12\",\"BOOL\":\"TRUE\",\"DR\":\"DRR001\",\"DRLT\":\"OO\"}]}"

 

Does anyone know how what I am doing wrong? Why I am not getting the result as a JSON data but a string?

ACCEPTED SOLUTION

Accepted Solutions

Hi Alex,

Since return type of your custom action is String you are getting the results as String. If you are expecting a JSON object define a complex type with all required attributes and return the complextype from your custom action/function. eg. below,

"returnType": {
"type": "CUSTOM.CustomDomain.TngWorx",
"isCollection": true
}

in this case TngWorx is name of my complex type created under customdomain. attached is the sample JSON file for defining complex type. change extension of file to JSON.

 

below is sample code snippet how you can assign values to each of attribute(Name, Number) and return the JSON object as action result from your action,

var complexValue = new ComplexValue();
complexValue.getValue().add(new Property("String", "Name", ValueType.PRIMITIVE, success[0]));
complexValue.getValue().add(new Property("String", "Number", ValueType.PRIMITIVE, success[1]));

var entity = new EntityAttribute(null, 'TngWorx', PropertyValueType.COMPLEX, complexValue);
var ActionResult = Java.type("com.ptc.odata.core.entity.processor.ActionResult");
var result = new ActionResult();
// var retEntity = parseIE(task);
result.setReturnedObject(entity);
return result;

 

Here the array success contains the required values for name and number. Hope this helps!!

 

Thanks,

Mahesh Namange

View solution in original post

2 REPLIES 2

Hi Alex,

Since return type of your custom action is String you are getting the results as String. If you are expecting a JSON object define a complex type with all required attributes and return the complextype from your custom action/function. eg. below,

"returnType": {
"type": "CUSTOM.CustomDomain.TngWorx",
"isCollection": true
}

in this case TngWorx is name of my complex type created under customdomain. attached is the sample JSON file for defining complex type. change extension of file to JSON.

 

below is sample code snippet how you can assign values to each of attribute(Name, Number) and return the JSON object as action result from your action,

var complexValue = new ComplexValue();
complexValue.getValue().add(new Property("String", "Name", ValueType.PRIMITIVE, success[0]));
complexValue.getValue().add(new Property("String", "Number", ValueType.PRIMITIVE, success[1]));

var entity = new EntityAttribute(null, 'TngWorx', PropertyValueType.COMPLEX, complexValue);
var ActionResult = Java.type("com.ptc.odata.core.entity.processor.ActionResult");
var result = new ActionResult();
// var retEntity = parseIE(task);
result.setReturnedObject(entity);
return result;

 

Here the array success contains the required values for name and number. Hope this helps!!

 

Thanks,

Mahesh Namange

alexisraj
4-Participant
(To:MN_9573936)

Yes, it does help. Thanks, Mahesh.

Announcements

Top Tags