The MSPro Boomi Collection
About
ScriptEase For Boomi
ScriptEase For Boomi
  • ScriptEase
    • ScriptEase Development Environment
    • What AI says!
      • The limits of AI ...
  • Software Installation
    • Install a local Atom
  • Project Setup
    • IntelliJ Configuration
    • Verify Project Setup
  • Concepts
    • Use Test Data
  • Examples
    • 1 - Debug an existing process script
    • 9 - Aggregate Prices Example
  • Test Contexts
    • The Process Call Chain
  • Download
  • 💤Appendix
    • Script Templates
    • Java thoughts and recommendations
      • Chose newer JVM for local development
    • Boomi documentation and links
    • Initialize IntelliJ Templates
    • Script Contexts
  • Troubleshoot
    • Java the weed
    • ClassNotFoundException - GroovyStarter
    • Invalid VCS root mapping
    • An illegal reflective access operation has occurred
    • UnauthorizedAccess Error
  • Licensing
    • License Activation
    • Privacy Policy
Powered by GitBook
On this page
  • Step-By-Step
  • Setup a Test Context
  • Set Test expectations
  • Implement the Script
  • Use the script
  1. Examples

9 - Aggregate Prices Example

Developing a real world script - using ScriptEase

Last updated 6 months ago

The Script and the Test code is available in your project in the training folder!

The Use-Case

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 ]
    }
]
Example Documents

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
}
Expected Result
[
    {
        "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 ]
    }
]
Script setup

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).

This is simple as that:

def context = new ProcessScriptContext(
  inputDocuments: [
    Document.fromText('{ "articleNo" : "a001", "price" : 1.23 }'),
    Document.fromText('{ "articleNo" : "a002", "price" : 2.00 }'),
    Document.fromText('{ "articleNo" : "a001", "price" : 1.02 }'),
    Document.fromText('{ "articleNo" : "a001", "price" : 0.98 }')
  ])
_testScript.run(context)

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.

println("\r\n--- Test Output ----------")
int docCount = context.outputDocuments.size()
println(docCount + " Document(s) after script execution")

assert 1 == docCount // <<<<<< expect one document

Implement the Script

Maintaining the script header information is mandatory, especially the date and author tags. This is even more important when you work in a team!

The Script Header
/* **************************************************************************
    Aggregate prices on JSON documents.
        
    IN : JSON documents of profile j.Price
         { "articleNo" : "a001", "price" : 1.23 }
         
    OUT: JSON documents of profile j.Prices (plural=array)
        [
		{
		        "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 ]
		}
        ]
    
    ------------------------------------------------
    13.06.2024  mspro -   Created
************************************************************************** */

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.

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

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

// *********** Document related functionality ************
Map jsonDoc = js.parseText( textDoc)
_logger.info( "DOC[$docNo]: ${jsonDoc.articleNo} = ${jsonDoc.price}")
// ******** end of Document related functionality ********

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

Test your Script in Boomi AtomSphere

and use it in your test process.

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

// https://www.tutorialspoint.com/groovy/groovy_lists.htm
List articles = []

// Documents loop
for (int docNo = 0; docNo < docCount; docNo++) 
{
		...
		
		
		// *********** Document related functionality ************
		
		Map jsonDoc = js.parseText(textDoc)
		_logger.info("DOC[$docNo]: ${jsonDoc.articleNo} = ${jsonDoc.price}")
		
		// Business Logic
		
		// Check if there is already an item with the same articleNo.
		// If yes, we must update that item. 
		// If no, we create a new item and add it to the prices list.
		// it - iterator = List element (of type)
		
		def article = prices.find { it.articleNo == jsonDoc.articleNo }
		// article TYPE 
		// {    
		//      articleNo
		//      minPrice, maxPrice, priceCount, 
		//      prices[]
		// }
		
		if (article == null) {  // << set breakpoint here
			articles.add([
					articleNo: jsonDoc.articleNo,
					priceCount : 1,
					minPrice : jsonDoc.price,
					maxPrice : jsonDoc.price,
					prices : [ jsonDoc.price]
			])
		} else { }
		
		// ******** end of Document related functionality ********

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.

Amend you code again and implement the else branch.

if (article == null) { // << set breakpoint here
	articles.add([
			articleNo: jsonDoc.articleNo,
			priceCount : 1,
			minPrice : jsonDoc.price,
			maxPrice : jsonDoc.price,
			prices :  [ jsonDoc.price]
	])
} else {
	// article found -> update
	article.priceCount++        // increment
	if( jsonDoc.price < article.minPrice) article.minPrice = jsonDoc.price 
	if( jsonDoc.price > article.maxPrice) article.maxPrice = jsonDoc.price
	article.prices.add( jsonDoc.price ) // We do _not_ check if the same articles already exists!
}

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.

	// ******** end of Document related functionality ********
}   // documents loop

// Your process related code (process properties etc.) here
String outputDoc = JsonOutput.toJson(articles)
_setTextDocument(dataContext, outputDoc, new Properties()) // <<<<<

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

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.

We parse the text document into a JSON (map), so that we can access the JSON elements as properties (see ). 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().

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

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

Working with JSON
The documents passed to the script are defined in the Test class
JsonSlurper instance
IntelliJ adding Imports
Prepare to debug the script
Debugging the script
Variables
The Test Class output
Script Code in Boomi
q.e.d.