Given two points on planet Earth, calls out to a Google API which gives the shortest driving distance.

[{"$type":"CommentBlock","x":92,"y":154.00001525878906,"parts":{"comment":{"value":"Code for a safe function \"DistanceViaGoogleMaps\""},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Function inputs: \"start\" (text), \"end\" (text)"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Function output: (number)"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":""},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"TEST DATA 1: should give 134 miles"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"{\n \"start\": \"Austin, TX\",\n \"end\": \"London, TX\"\n}"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"TEST DATA 2: should give a null (impossible driving distance)"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"{\n \"start\": \"Austin, TX\",\n \"end\": \"London, UK\"\n}"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":""},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"base static text for the web service"},"next":{"$type":"DeclareVariableBlock","parts":{"itemVar":{"value":"UrlString"},"type":{"value":"text"},"default":{"$type":"LiteralStringBlock","parts":{"text":{"value":"https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington,DC&destinations=Chicago,IL&key=YourKeyHere"}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"lookup our authentication key"},"next":{"$type":"CallSafeFunctionBlock","parts":{"function":{"value":15},"return":{"value":"ourKey"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Insert dynamic parameters into static text"},"next":{"$type":"SetVariableBlock","parts":{"variable1":{"value":"UrlString"},"value":{"$type":"StringReplaceBlock","parts":{"search":{"$type":"LiteralStringBlock","parts":{"text":{"value":"YourKeyHere"}}},"replace":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ourKey"}}},"in":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"UrlString"}}}}},"next":{"$type":"SetVariableBlock","parts":{"variable1":{"value":"UrlString"},"value":{"$type":"StringReplaceBlock","parts":{"search":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Washington,DC"}}},"replace":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"start"}}},"in":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"UrlString"}}}}},"next":{"$type":"SetVariableBlock","parts":{"variable1":{"value":"UrlString"},"value":{"$type":"StringReplaceBlock","parts":{"search":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Chicago,IL"}}},"replace":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"end"}}},"in":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"UrlString"}}}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"call out to web service"},"next":{"$type":"DeclareVariableToNewTypeBlock","parts":{"$typeName":"RestClient","$variableName":"restClient","$next":{"$type":"WebServiceCallBlock","parts":{"url":{"$type":"LiteralStringBlock","parts":{"text":{"value":""}}},"serviceObj":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"restClient"},"variable2":{"value":""}}},"method":"GET","outputVariable":"MapsCallResponse","ensureSuccessStatusCode":true,"contentType":"application/json","headers":[],"parameters":[],"next":{"$type":"IfBlock","parts":{"v":2,"if":{"$type":"CompareBlock","parts":{"left":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"MapsCallResponse"},"variable2":{"value":"statusCode"}}},"operator":{"value":"!="},"right":{"$type":"NumberBlock","parts":{"number":{"value":"200"}}}}},"then":{"$type":"ReturnBlock","parts":{"value":{"$type":"NullBlock"}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"an example of the result string (which we need to parse):"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"{\n \"destination_addresses\": [\n \"London, TX 76854, USA\"\n ],\n \"origin_addresses\": [\n \"Austin, TX, USA\"\n ],\n \"rows\": [\n {\n \"elements\": [\n {\n \"distance\": {\n \"text\": \"215 km\",\n \"value\": 215180\n },\n \"duration\": {\n \"text\": \"2 hours 18 mins\",\n \"value\": 8278\n },\n \"status\": \"OK\"\n }\n ]\n }\n ],\n \"status\": \"OK\"\n}"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":""},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"drill down into this complex results string from the web service, getting just what we need:"},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"ResponseJson"},"type":{"value":"map"},"default":{"$type":"JsonDeserializeBlock","parts":{"type":{"value":"map"},"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"MapsCallResponse"},"variable2":{"value":"body"}}}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"rows"},"type":{"value":"map[]"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFromMap","parts":{"map":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ResponseJson"}}},"key":{"$type":"LiteralStringBlock","parts":{"text":{"value":"rows"}}}}},"type":{"value":"map[]"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"firstRow"},"type":{"value":"map"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFirstOrLastInArrayBlock","parts":{"firstOrLast":{"value":"first"},"arrayVar":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"rows"}}}}},"type":{"value":"map"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"elements"},"type":{"value":"map[]"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFromMap","parts":{"map":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"firstRow"}}},"key":{"$type":"LiteralStringBlock","parts":{"text":{"value":"elements"}}}}},"type":{"value":"map[]"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"firstElement"},"type":{"value":"map"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFirstOrLastInArrayBlock","parts":{"firstOrLast":{"value":"first"},"arrayVar":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"elements"}}}}},"type":{"value":"map"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"status"},"type":{"value":"text"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFromMap","parts":{"map":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"firstElement"}}},"key":{"$type":"LiteralStringBlock","parts":{"text":{"value":"status"}}}}},"type":{"value":"text"}}},"next":{"$type":"IfBlock","parts":{"v":2,"if":{"$type":"CompareBlock","parts":{"left":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"status"}}},"operator":{"value":"=="},"right":{"$type":"LiteralStringBlock","parts":{"text":{"value":"OK"}}}}},"then":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"distance"},"type":{"value":"map"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFromMap","parts":{"map":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"firstElement"}}},"key":{"$type":"LiteralStringBlock","parts":{"text":{"value":"distance"}}}}},"type":{"value":"map"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"distanceInMeters"},"type":{"value":"number"},"default":{"$type":"CastBlock","parts":{"value":{"$type":"GetFromMap","parts":{"map":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"distance"}}},"key":{"$type":"LiteralStringBlock","parts":{"text":{"value":"value"}}}}},"type":{"value":"number"}}},"next":{"$type":"DeclareVariableBlock","disabled":false,"parts":{"itemVar":{"value":"distanceInMiles"},"type":{"value":"number"},"default":{"$type":"RoundBlock","parts":{"mode":{"value":"Math.round"},"value":{"$type":"MathOperationBlock","parts":{"v":2,"parts":[{"$type":"GetVariableBlock","parts":{"variable1":{"value":"distanceInMeters"}}},"*",{"$type":"NumberBlock","parts":{"number":{"value":"0.000621371"}}}]}}}},"next":{"$type":"ReturnBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"distanceInMiles"}}}}}}}}}}},"else":{"$type":"ReturnBlock","parts":{"value":{"$type":"NullBlock"}}}}}}}}}}}}}}}}}}}}}}}}}}}}},"$propertySettings":{"baseUrl":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"UrlString"}}}}}}}}}}}}}}}},"returnValue":"text"}}}}}}}}}}}}}}}}}}}}}}}}}}](triple-click to select this code. Then copy, open a safe function snap workspace, and right-click in the workspace to “paste JSON” it into your Epicor CPQ instance.)
Some things to note about this code:
This code only works within a safe function.
Since we’re calling a web service, we must use a safe function to hold this Snap code. If you paste the JSON above into another Snap workspace (such as a value rule), you’ll be warned that some blocks could not be pasted. Of course, any configurator, quote, workflow, or scene can call this safe function to get the results.Calling Google’s APIs requires setting up a key.
This computer-generated text you get from your Google API account helps ensure the account is being used appropriately. In the code example, you see how we placed our API key into a second safe function, so even if we use this key in many safe functions or web service callouts, we only have to update it in one place.Powerful web services can result in complex JSON you need to parse.
The result from this Google web service is a complex JSON string with many pieces of information. We just want one element from that structure, like picking a leaf from a tree. Snap makes it easy to serialize data structures into JSON text, and deserialize them back out again. The code here deserializes (converts) the JSON text into a logical hierarchy of maps and arrays, then drills down through the layers of data to get just the value we need. In this case, we’re not interested in those data structures, so we cast them into variables that are simply forgotten. If you get a complex data structure you want to keep, you can create a type or a global type that matches the structure, and then create and populate variables of that type to store the info.