vchhoa | 2021-02-02 10:43:42 UTC | #1
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 at platform.client.v2@112.0.1 at platform.client.v2@112.0.1 at platform.client.v2@112.0.1
I tried another JSON API, json-simple, and got the same error.
Anyone managed to build a proper "JSON-ized" object ?
Cheers, Valéry
anon11147534 | 2021-02-02 11:15:13 UTC | #2
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.
vchhoa | 2021-02-02 13:32:07 UTC | #3
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 ?
anon11147534 | 2021-02-02 14:03:09 UTC | #4
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.
system | 2021-03-05 14:03:09 UTC | #5
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 9867