Unable to update a datatable row using Java SDK

Hello there,

Just wondering if someone have already use the Java SDK to update or insert row into a datatable ...

I have been developping java code for customer where I need to update a row of a datatable. I'm calling the following method :

Map<String, Object> com.mypurecloud.sdk.v2.api.ArchitectApi.putFlowsDatatableRow(String datatableId, String rowId, Object body) throws IOException, ApiException

Update a row entryUpdates a row with the given rowId (the value of the key field) to the new values. The DataTableRow should be a json-ized' stream of key -> value pairs { "Field1": "XYZZY", "Field2": false, "KEY": "27272" }

Parameters:
datatableId id of datatable (required)
rowId the key for the row (required)
body datatable row (optional)

Returns:
MapThrows:ApiException - if the request fails on the server
IOException - if the request fails to be processed

At first, I tried to use a String for the body:

		String body = "{ " +
			  "\"LastExecutionDate\": \"" + lastExecutionDate + "\", " +
			  "\"LastExecutionStatus\": \"" + lastExecutionStatus + "\", " +
			  "\"LastExecutionErrorMsg\": \"" + lastExecutionDate + "\", " +
			  "\"ContactListId\": \"" + contactListId + "\", " +
			  "\"CSATUpperLimit\": \"" + CSATUpperLimit + "\", " +
			  "\"LastSurveyHandledDate\": \"" + lastExecutionDate + "\", " +
			  "\"key\": " + rowId +  "} ";

But I'm getting an error on the execution.

So I looked up on Google on how to create a "JSON-ized" object and found multiple API for that.
I tried using json.org and my code looks like this :

ArchitectApi architectApiInstance = new ArchitectApi();

JSONObject body = new JSONObject();
body.put("LastExecutionDate", lastExecutionDate);
body.put("LastExecutionStatus", lastExecutionStatus);
body.put("LastExecutionErrorMsg", lastExecutionDate);
body.put("ContactListId",contactListId);
body.put("CSATUpperLimit", CSATUpperLimit);
body.put("LastSurveyHandledDate",lastExecutionDate);
body.put("key",rowId);

System.out.println("body = " + body);

architectApiInstance.putFlowsDatatableRow(datatableId, rowId, body);

When executing it, the output looks good:

body = {"LastExecutionDate":"","LastExecutionStatus":"PROCESSING","LastExecutionErrorMsg":"","ContactListId":"","CSATUpperLimit":1,"key":"UpdateBadCSATContactList","LastSurveyHandledDate":""}

But the code is throwing the following exception (same error as when using String object)

com.mypurecloud.sdk.v2.ApiException: error
at platform.client.v2@112.0.1/com.mypurecloud.sdk.v2.ApiClient.interpretConnectorResponse(ApiClient.java:567)
at platform.client.v2@112.0.1/com.mypurecloud.sdk.v2.ApiClient.getAPIResponse(ApiClient.java:580)
at platform.client.v2@112.0.1/com.mypurecloud.sdk.v2.ApiClient.invoke(ApiClient.java:673)
at platform.client.v2@112.0.1/com.mypurecloud.sdk.v2.api.ArchitectApi.putFlowsDatatableRow(ArchitectApi.java:8416)

I tried another JSON API, json-simple, and got the same error.

Anyone managed to build a proper "JSON-ized" object ?

Cheers,
Valéry

Hi,

You can log the getRawBody() and getStatusCode() output of the ApiException being thrown. That will give you a good indication of what's gone wrong.

See the documentation for PUT /api/v2/flows/datatables/{datatableId}/rows/{rowId} to get a description of the error. The api-explorer may also be helpful in crafting the correct JSON body for the request.

Hello Ronan,
Thanks for the hint.

So, using the json.org API, I have the following error:

Exception while reading Jobs_Result DataTable: com.mypurecloud.sdk.v2.ApiException: error
RawBody: {"message":"'' is too short\n\nFailed validating 'minLength' in schema['properties']['key']:\n {'$id': '/properties/key',\n 'displayOrder': 0,\n 'maxLength': 256,\n 'minLength': 1,\n 'title': 'JobName',\n 'type': 'string'}\n\nOn instance['key']:\n ''","code":"flows.datatables.schema.exception","status":400,"messageParams":{},"contextId":"0112742f-7c0a-4a3a-b266-5c0df8323007","details":[],"errors":[]}
StatusCode: 400

The JSON body looks good. The "key" field is present and filled and is working using the API Explorer:

body = {"LastExecutionDate":"","LastExecutionStatus":"PROCESSING","LastExecutionErrorMsg":"","ContactListId":"","CSATUpperLimit":1,"key":"UpdateBadCSATContactList","LastSurveyHandledDate":""}

But using the json.simple API, it is working now.
And the body looks like the same :

body = {"LastExecutionDate":"","LastExecutionStatus":"PROCESSING","LastExecutionErrorMsg":"","ContactListId":"","CSATUpperLimit":1,"key":"UpdateBadCSATContactList","LastSurveyHandledDate":""}

I guess there's an issue with the json.org API then ...

Thanks Ronan !

Quick question, what is the recommanded Json API to use to format a JSON object ?

Glad to see it's working now. I'm not sure if we recommend any particular JSON formatting libraries. Judging by the fact that the json.simple library worked over the json.org library, I would be recommending that in future to anyone who asks.

Anecdotally, Google's GSON library has a good usage base amongst Java developers.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.