9 - Aggregate Prices Example

Developing a real world script - using ScriptEase

circle-check
chevron-rightThe Use-Casehashtag

We have any number of incoming JSON documents of the following profile (type):

{
  "articleNo" : "string",
  "price" : 1.23
}

There can be more than one document referring to the same article number but a different price.

All documents should be consolidated into one outbound document containing an array of unique articles as follows:

[
    {
        "articleNo" : "...",
        "maxPrice"  : 1.23,
        "minPrice"  : 0.98,
        "priceCount" : 2,
        "prices" : [ 0.98, 1.23 ]
    }
]
chevron-rightExample Documentshashtag

Four documents referring to two different articles

Document 1

{
  "articleNo" : "a001",
  "price" : 1.23
}

Document 2

{
  "articleNo" : "a002",
  "price" : 2.00
}

Document 3

{
  "articleNo" : "a001",
  "price" : 1.05
}

Document 4

{
  "articleNo" : "a001",
  "price" : 0.98
}
chevron-rightExpected Resulthashtag
[
    {
        "articleNo" : "a001",
        "maxPrice"  : 1.23,
        "minPrice"  : 0.98,
        "priceCount" : 3,
        "prices" : [ 1.23, 1.05, 0.98 ]
    },
    {
        "articleNo" : "a002",
        "maxPrice"  : 2.00,
        "minPrice"  : 2.00,
        "priceCount" : 1,
        "prices" : [ 2 ]
    }
]
chevron-rightScript setuphashtag

Create a folder (aka Package) called AggregatePrices

and add a Script and a Test Script from a Script Templates

Step-By-Step

Setup a Test Context

The first things we need to do is to setup a test context that provides the test data, as it is normally provided by the Boomi run-time. In our case, we must create the documents that we want to pass into the script for testing( see Example Documents).

The documents passed to the script are defined in the Test class

This is simple as that:

circle-info

We do not need any Execution Property or Dynamic Property or Process Property in our script, but the Context would the right place to add those.

Dynamic Document Properties would be added to each document, not to the Context itself.

Set Test expectations

Unlike in the template, we expect only one document as output, and we have to amend our test assertion in the Test class. Feel free to add more assertions to ensure the script works as expected.

Implement the Script

triangle-exclamation
chevron-rightThe Script Headerhashtag

We need the JsonSlurper object to work with JSON data. We create the js object instance before the documents loop because there is no need to have a new instance for each document. One js is enough for all documents.

JsonSlurper instance

You recognize that IntelliJ is smart enough to automatically insert the requires Imports:

IntelliJ adding Imports

Navigate to the script logic and replace Your document related code here ... with:

We parse the text document into a JSON (map), so that we can access the JSON elements as properties (see Working with JSONarrow-up-right). The scipt's logic so far is to log the incoming data. Good enough for a first Debug Run. Set a breakpoint on line 52, navigate to the Test and Debug test01().

Prepare to debug the script

The execution stops at the breakpoint. See the variables and output.

Debugging the script
chevron-rightTest your Script in Boomi AtomSpherehashtag

You can copy and paste the script code into the psgAggregatePricesscript component

and use it in your test process.

Check the process logs to see the log entires your script has written

Let's improve the scripts functionality and build the business logic. Amend your code, define the articles result list and business logic, and set a breakpoint on the line // << set breakpoint here

Debug your script and see the execution stopping with article == null. This is expected because - look at the Variables window - the priceslist is yet emtpy.

I think, you will find out yourself how to step over the next two lines (Single Step - F10) to stop at _setTextDocument(). While your colors might be different (depending in the IntelliJ theme you have chosen), the red line is the breakpoint and the yellow line marks the next line for execution.

More interesting to see is the Variables window where you can observe that the expect article object was added to the articles list.

Variables

Amend you code again and implement the else branch.

Set a breakpoint on the _setTextDocument() line and debug a bit: document by document. After the third document you should check the console output and the variables. You will recognize the variables are close to what we expect to see in the output JSON.

Last but not least, we must write back the articles map into one output document. So, you must move the _setTextDocument() to outside of the document loop. Before, we must convert the articles map to a Json String.

As the very last step, you can update your Test class and prettyPrint() the returned Json document:

The Test Class output

Use the script

Do you rememeber why we did all that effort? We wanted to use that script in Boomi! Copy and paste all the script code 1:1 into the script component. Of course, the Test class is not needed in Boomi.

Script Code in Boomi
q.e.d.

Last updated