
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 04-27-2021 09:00 AM
There's a long simmering debate about low-code/no-code vs code but I've yet to find a solution that doesn't require at least a little scripting or code. Here was my chance to brush up on my javascript skills a little bit and still accomplish the task at hand. The low code solution of Flow Designer takes care of everything else for you and leaves me with only one coding challenge to overcome. The main lesson for me is to code less and test more and the solution will come quicker.
Scenario: a REST API responding with JSON and within that JSON is an array of data. ServiceNow Flow Designer makes it really easy. I'm also making it easy on myself in that this is just a demo solution and I am going to grab the first chunk of data from the array. Here I am in the script step after the REST API call, I've grabbed the response Body into the input variable.
I've seen examples where the developer will put an "if response == 200" then it'll go into parse but this is the fun thing about demo - I'm pretty sure this is going to work all the time - I'm not really sweating the details.
One of the realizations I had in this process was to build the parse from the bottom up and not try to get it in the first shot.
Let me show you how I came to my conclusion. So I am going to show you the process in reverse. You can see the destination in the script, however, there are some great landmarks on the way to the destination.
(function execute(inputs, outputs) {
// ... code ...
var usageRecords = JSON.parse(inputs.responseBody);
outputs.quantity_of_messages = usageRecords//.usage_records[0].count;
// outputs.units_expended = 1.3 * usageRecords.usage_records[0].price;
})(inputs, outputs);
The output variable at this stage is simply the API response, I am parsing the response body, declaring it to the usageRecords var and then assigning it to the output variable quantity_of_messages. It's just a pass through at this point. A sanity check.
Yes, this is what we're expecting. Now let's move on to the next step, we need to pull data from the usage_records array, square brackets denote array, and an array is a list. In this example it's almost always going to be one item array so I feel good about just grabbing the first item [0] and then key value pair out of that array. Stick with me because there's something cool that happens.
In the code I have moved the comment forward to the usage_records array - compare this to the code above - there's just a minor change in moving the // forward a little.
(function execute(inputs, outputs) {
// ... code ...
var usageRecords = JSON.parse(inputs.responseBody);
outputs.quantity_of_messages = usageRecords.usage_records//[0].count;
// outputs.units_expended = 1.3 * usageRecords.usage_records[0].price;
})(inputs, outputs);
Notice in the background I have [object Object] circled - that square bracket denotes I'm on the array.
Now I roll the comment forward past the [0] and we get the first instance of the array, it's also the only instance in the array, it makes our task easier.
From here I can duplicate my success and pick off the Keys and therefore values of the JSON array.
Now I'll cut out all of the comments and you can see the output variables and even with a calculation multiplied to one of them as output.
(function execute(inputs, outputs) {
// ... code ...
var usageRecords = JSON.parse(inputs.responseBody);
outputs.quantity_of_messages = usageRecords.usage_records[0].count;
outputs.units_expended = 1.3 * usageRecords.usage_records[0].price;
})(inputs, outputs);
The results!
That's it! Just walk the output variable forward slowly through the object array and key:value of JSON.
I hope I take my own advice moving forward instead of just trying to get it on the first try. I would have saved myself some time. It costs nothing to test.
- 656 Views