Nathan_Tossens | 2021-03-04 08:09:50 UTC | #1
Dears,
I'm currently working on a Data Action to retrieve the wrapup note from a conversation to have it availale in the script.
The data action is quite simple, using /api/v2/conversations/${input.conversationID}.
As input contract, I only have de conversation ID.
In the response I get from the API , I can easily navigate to the Wrapu Notes via this Json Path :
$.participants[*].wrapup.notes
My problem is that, when there are multiples notes, How can I deal with that? I would like to be able to retrieve all the notes but as result of $.participants[*].wrapup.notes is an array I don't know how to perform that.
To get the first note, I used de successTemplateUtils.FirstFromArray but how can I get the other?
Here is the example of the result of $.participants[*].wrapup.notes : [ "Test de note 1\n", "Test de la note 2" ]
Here is my Response Template :
{ "translationMap": { "note1": "$.participants[*].wrapup.notes" }, "translationMapDefaults": {}, "successTemplate": "{\r\n \"note1\": ${successTemplateUtils.firstFromArray(\"${note1}\")}\r\n}" }
Please advise :slight_smile:
Regards,
Nathan
Jason_Mathison | 2021-03-04 12:44:10 UTC | #2
Hi Nathan,
My understanding is that scripts don't support arrays, so we pretty much would have to brute force this in the Data Action. Here is an approach to handling similar JSON. It will display up to 3 brands, but could be scaled up to pretty much any number. Note that since there isn't a 3rd brand, the #if will prevent anything from being output for that.
I believe that Jerome Saint-Marc will be posting a different approach that can handle any number of notes, and doesn't require a bunch of copy and paste, but will not end up with the output broken up into
{ "Note1" : "Note 1 content", "Note2" : "Note 2 content" }
Here is the example JSON that I worked with:
{ "mobilePhoneNumber" : "11111", "emailAddress" : "abc@null.com", "contractCount" : 3, "contractList" : [ { "brand" : "testbrand", "contractType" : "contractType", "contractId" : "contractId", "reinsurer" : "reinsurer" }, { "brand" : "testbrand1", "contractType" : "contractType1", "contractId" : "contractId1", "reinsurer" : "reinsurer1" }] }
Here is the response configuration to create individual entries for entries that exist:
{ "translationMap": { "brand1": "contractList[0].brand", "brand2": "contractList[1].brand", "brand3": "contractList[2].brand" }, "translationMapDefaults": { "brand1" : "", "brand2" : "", "brand3" : ""}, "successTemplate": "{ #if($brand1 != \"\") \"brand1\" : $brand1\n#end #if($brand2 != \"\") ,\"brand2\" : $brand2\n#end #if($brand3 != \"\") ,\"brand3\" : $brand3\n#end }" }
--Jason
Jerome.Saint-Marc | 2021-03-04 13:25:01 UTC | #3
Hello,
As Jason just mentioned it above, I have just tried another approach. But it comes with certain constraints - or it depends how/if you are planning to leverage notes individually in your script. In this case, you might have to change some of the string replace I have put in this example below (inside the Data Action).
So the principle is to transform an array you have obtained (via JSONPath expression) into a string (with a separator). Your Data Action output contract property will be a string.
If ever you need to work on some elements of the array in your Script (specific ones), you could leverage Dynamic variables in Script, which allow to use certain string functions at this level. As an example, you can use a combination of indexOf and substr or slice to split based on a comma or another separator: https://help.mypurecloud.com/articles/additional-functions-to-use-in-dynamic-variables/
Back to the Data Action:
In my output contract, I have something like this:
{ "type": "object", "properties": { "notes": { "type": "string" } }, "additionalProperties": true }
In my Response configuration, in the translationMap, I define the JSON expression: "notesArrayAsStr": "$.participants[*].wrapup.notes" I have used ArrayAsStr in this variable name (not a keyword), just to explain that the result of the translationMap and JSONPath expression will be an array, but which is in fact treated as a string in the Data Action Response.
I also define a default value for the notesArrayAsStr in my translationMapDefaults - in case the JSONPath expression returns an empty array, or in case the JSONPath expression fails (attributes in the path not present anywhere): "notesArrayAsStr": "[]"
As an example (with 3 notes, the first one also containing a comma to make it more complex and to illustrate the fact that replacing comma with another separator can be "dangerous"): "notesArrayAsStr": "[ \"xxxx, dsfdsfdsf\", \"yyyyyy\", \"zzzzzz\" ]"
In my successTemplate, I use a mix of Velocity commands (#set), to manage a temporary variable which will allow me to manipulate the notesArrayAsStr variable (using String macros) before building the response. Using: https://help.mypurecloud.com/articles/velocity-macros-data-actions/#JavaStringMethods And: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
What I want is to remove [ and ], remove leading and trailing spaces, replace `\", ` (including a space after the comma) with my new separator (I have chosen | but you could use what you want), and remove the remaining `\"`. |
The purpose is to get: ```"notes": "xxxx, dsfdsfdsf | yyyyyy | zzzzzz"``` |
So here is my full Response Configuration:
{ "translationMap": { "notesArrayAsStr": "$.participants[*].wrapup.notes" }, "translationMapDefaults": { "notesArrayAsStr": "[]" }, "successTemplate": "#set ($notesConcatenated = $notesArrayAsStr.replace(\"[\", \"\").replace(\"]\", \"\").strip().replace('\", ', \" | \").replace('\"', \"\")) {\"notes\": \"${notesConcatenated}\" }" }
The drawback is that if you need to access individual/specific notes, you will need to manipulate the string again in the Script with the dynamic variable and functions (as I mentioned at the beginning of my post).
Regards,
Nathan_Tossens | 2021-03-04 13:41:59 UTC | #4
Jason, Jerome,
first of all thanks a lot for your answers. Both of them were really useful.
In that particular case, I'll need to use the answer of Jerome because I can't know in advance how many notes I can have attached to a call.
Jerome, this is brilliant thanks :) I didn't even thought about parsing the Array to a string I was more focus on extracting the info via JSONPath :stuckouttongue:
Anyway, thanks a lot to both of you!
Regards,
Nathan.
Jerome.Saint-Marc | 2021-03-04 13:54:25 UTC | #5
Jason :mage: was the one to teach me this parsing method :-)
system | 2021-04-04 13:51:49 UTC | #6
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: 10167