ottoflux | 2020-07-27 21:30:30 UTC | #1
Hi everyone - So, I'm hoping to explain this better than some other articles I've seen here by focusing on the basic concept of getting a naked array (unwrapped array, native array?) in JSON from a web service and getting PureCloud/Genesys Cloud (do we just call it "Cloud" now?) to consume it.
Let's pretend we're getting this data back (don't have a heart attack, it's fake data):
[ { "ssn": "987654321", "birthDate": "01011970" }, { "ssn": "123456789", "birthDate": "01021970" } ]
Also you can hit my mockable here: https://demo7763278.mockable.io/fake for that in case it helps to set this up yourself.
TL;DR;? You can also lift an exported sample from my pastebin.
Ok, so, that's totally valid JSON - but it can't be flattened because $prefix Cloud doesn't have a way to address it.
If you don't believe me, go ahead (without doing what I'm about to show you) and try to make it work as a {$rawResult} entry. It won't. But I'm getting ahead of myself.
So, a lot of times in the past (as a developer, and therefore lazy) I'd just ask the person sending it to wrap it in an object so $prefix Cloud can be happy. Like this:
{ "things" : [ { "ssn": "987654321", "birthDate": "01011970" }, { "ssn": "123456789", "birthDate": "01021970" } ] }
Hooray! Now we can get flattened data and the engineers won't be mad at me for slowing up their progress on call flows.
But wait! That's great if the developer can do that, but that's not always possible (because I'm not the only lazy developer, and that's fair, because as I said at the top it's a good valid JSON array).
Now there's some output config trickery you can do that's hinted at in https://developer.mypurecloud.com/forum/t/process-multiple-array-of-contacts-or-accounts-from-a-salesforce-data-action/2274 but I feel like it's a bit confusing and Force Force centric. So I made this example that's nice and plain. Except for all the asides. Hi.
Alright, so we have our naked array and $prefix Cloud needs some help.
So, in comes our Configuration tab and we get to use our hackery [sic*] for the rescue.
Configuration -> Response:
{ "translationMap": { "stuff": "$.*" }, "translationMapDefaults": {}, "successTemplate": "{\"results\": ${stuff}}" }
Ta Da! Oh, wait. Explanations. So. $.* is like "hey, give me the stuff, like all the stuff" which is why I named it "stuff" and because having different variable names that aren't related to each other can come in handy for clarity. :eyes:
So now at this point ${stuff} holds our naked array. Now we take it and put it in a fancy "results" object in the "successTemplate" - note the *escaped quotes* above so we can make new JSON from our old JSON.
Hooray! Now in your output contract you can say "hey, give me the object with an array named results (because we just named it that) of objects with these two strings (ssn, birthDate) in it".
Basically -> Naked array result (shoves into Object->Array("results")->Object->string (ssn, birthDate)
Don't forget you can just import all this from my pastebin link up top at TL;DR; but you did it, you made it to the bottom!
Buy me a beer or bourbon at the next conference?
Let me know if you have questions, comments or if this helped or entertained you at all.
Thanks - .jason r. totten / (once Pablo) / $jrt
*sic Latin for thus as in as spelled or intended not to be corrected. Not SIC which was something different completely if anyone remembers that.
Jeremy_Gillip | 2020-07-28 15:32:08 UTC | #2
Nice, very helpfull!
Jerome.Saint-Marc | 2020-07-28 16:32:27 UTC | #3
Thank you for taking the time to write this and to share it with the community!
ottoflux | 2020-07-28 16:57:14 UTC | #4
You're welcome - I realize I should start doing this each time I fight with something so that it might make the next person's job a little easier.
Dan_Fontaine | 2020-08-03 13:36:45 UTC | #5
TL;DR; Thanks!
Entertaining and helpful. Thank you.
ottoflux | 2020-08-04 16:52:25 UTC | #6
Thanks - I'm glad it helped.
ottoflux | 2020-08-11 12:22:26 UTC | #7
ottoflux, post:1, topic:8403
"successTemplate": "{\"results\": ${stuff}}"
A point of clarification here - it doesn't have to be "results" it could be "cheese", it's just the name of the array, and you don't have to specify the array name in the contracts.
system | 2020-09-11 12:22:31 UTC | #8
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: 8403