GroovyScriptInlineReceiver
The GroovyScriptInlineReceiver executes an in-line Groovy script supplied through the Receiver's data rather than reading it from a file like the GroovyScriptReceiver. Attribute values are substituted into the script before execution; for each mapped Attribute, a {variable} property key defines the token in the script to replace with the Attribute's current value. The script can be run before, during, or after each row iteration.
GroovyScriptInlineReceiver vs GroovyScriptReceiver
- GroovyScriptInlineReceiver - Use this receiver for short, one-off utility scripts. The main advantage is that the script is stored directly in the GenRocket Scenario, so there is no need to maintain a separate script file on every system or server where the GenRocket Runtime is installed and used.
- GroovyScriptReceiver (External File) - Use this receiver for large or complex scripts. The script is maintained as a separate Groovy file, making it easier to develop, manage, update, and reuse.
Benefits
- Eliminates external script files by storing the custom Groovy script directly in the Receiver within Scenario.
- Provides a customized option when standard Receiver behavior does not meet a specific requirement.
- Provides flexible execution timing by allowing the script to run before, during, or after data generation.
- Supports row-level processing for logic that needs to be applied to each generated record.
- Supports pre- and post-processing tasks such as initializing values, validating output, or adding content after generation.
- Keeps custom logic with the Scenario, making it easier to understand and maintain how the output is produced.
- Simplifies deployment by reducing the number of external script files that must be distributed to Runtime environments or CI/CD pipelines.
- Speeds up development and testing by allowing short scripts to be modified without maintaining and redeploying a separate script file.
- Supports automated workflows because the custom logic executes automatically whenever the Scenario runs.
Use Cases
| Case | Description |
|---|---|
| Custom Data Transformation |
Apply custom logic to generated Attribute values when the required transformation is not available through the configured Generators or Receivers.
For example, convert generated status values such as Active and Inactive to 1 and 0 before writing them to the output. |
| Cross-Attribute Processing |
Use values from multiple Attributes to calculate or construct an output value. For example, evaluate state, itemCategory, and orderTotal to determine the appropriate value for a taxFlag Attribute. |
| Custom Output Formatting |
Construct output that requires specialized formatting or combinations of generated values. For example, combine multiple Attributes into a custom string: 12345|John Smith~ACTIVE
This can be useful when an application expects a proprietary or non-standard record format. |
| Output Validation |
Validate generated data or output as part of Scenario execution. For example, check that required values were generated or verify that generated records meet application-specific requirements. |
| Pre- and Post-Processing |
Perform an action before generation begins or after generation completes. For example, initialize information needed by the script before processing begins or add a custom footer after all records have been generated. |
| Self-Contained Scenario Logic |
Keep small amounts of custom processing logic directly with the Scenario instead of maintaining a separate Groovy script file. This is useful for CI/CD pipelines and Runtime environments where minimizing external dependencies simplifies deployment. |
Real World Examples
Example 1 - Legacy Banking File Format
- Requirement: A banking application expects transaction records to be in a proprietary format. Each record contains generated routing and account information along with a calculated value derived from those fields.
- Solution: GenRocket generates the required transaction data. The GroovyScriptInlineReceiver uses the generated Attribute values to perform the required calculation and construct the expected output for each record.
- Why use the GroovyScriptInlineReceiver? The calculation is specific to the target application's file format and can be performed as each generated record is processed.
Example 2 - Custom Healthcare Record Transformation
- Requirement: A healthcare testing environment requires generated records to contain an authorization identifier in a specific application-defined format before the records are written to the output file.
- Solution: GenRocket generates the underlying patient and authorization data. An inline Groovy script transforms the required values into the format expected by the downstream application.
- Why use the GroovyScriptInlineReceiver? The transformation is specific to the target application and can remain directly with the Scenario instead of requiring a separately deployed script.
Example 3 - Retail Tax Classification
- Requirement: A retail application requires a taxFlag value, which is determined from several generated fields, including state, itemCategory, and orderTotal.
- Solution: The GroovyScriptInlineReceiver evaluates the generated values for each record and applies the required business rules to determine the taxFlag.
- Why use the GroovyScriptInlineReceiver? The rule depends on multiple generated Attribute values and is easier to express as a small piece of custom logic than as a simple formatting operation.
Receiver Parameters
The following parameters can be defined for the GroovyScriptInlineReceiver. Items with an asterisk (*) are required.
- beforeLoop - Defines whether to run the script before the iteration of each row of generated data.
- whileLoop - Defines whether to run the script during the iteration of each row of generated data. Each of the Attribute's values will be passed as an argument to the Groovy Script in case of while loop.
-
afterLoop - Defines whether to run the script after the iteration of each row of generated data.
Note
Set only one loop option to ‘true’. Set the other two options to ‘false’. More than one can be set to true, and doing so will result in the Receiver executing the same inline script in every enabled phase.
Receiver Data - Script
Use this tab to enter the Groovy Script.
Script Character Limit - 4,000 characters
Example
Write generated first names to a CSV file, and then use a Groovy script to add a record-count footer.
def csvFile = new File("var1" + File.separator + "var2")
def recordCount = csvFile.readLines().size() - 1
csvFile.append(
System.lineSeparator() + "TOTAL_RECORDS=${recordCount}"
)
println "Added record count to ${csvFile.name}"Note
Performance depends on how the script is written, which is outside the scope of GenRocket. Scripts can be written in several ways, and how they are written can disrupt performance/data generation. Users are responsible for entering a functional, correct script and maintaining it.
Receiver's Attribute Property Keys
The Receiver defines only one property key that can be modified on any of its associated Domain Attributes:
- variable - Determines if the Attribute will be used as a variable within the update statement. Up to 20 Attributes may be assigned to a variable (var1 - var20). The default is ‘noVar.’ meaning the Attribute will not be used.

Usage Examples Overview
The GroovyScriptInlineRecever in these examples uses a script that counts the number of records in one or more CSV files and adds the following to each file. For example, if the CSV file contains 100 data records, the script adds at the end of the file:
TOTAL_RECORDS=100In this example, the GroovyScriptInlineReceiver uses two variables to read the file path and subpath (or subpath and file name).
-
var1 – the path to the output directory. The Attribute could use a ConstantGen with a value of #{resource.output.directory}. This will instantly use your resource output path value.
- var2 – the subpath or file name for the CSV file(s).
The script combines var1 and var2 to locate the CSV file. It then counts the data records in the file, excluding the header row, and appends the total record count to the end of the file.
Specific to Example
var2 can specify a path to a specific CSV file or a subdirectory containing multiple CSV files. The script used depends on whether you are processing a single file or multiple files in a specified directory.
While these examples use a CSV file, you can use another file type or a different action depending on your needs.
Example 1 - Add Record Count to Single CSV File
This example uses a Groovy Script to add the record count to a single CSV file stored in a defined path and subdirectory. The path points directly to the CSV file, which contains the following data:

Step 1 - Set up a Domain
Create a Domain containing two Attributes. For this example, the Domain is titled 'Script'.
-
path = Assign a ConstantGen Generator and references #{resource.output.directory}.
-
subPath - Assign a ConstantGen Generator and enter any path within outputDir
Step 2 - Add the GroovyScriptInlineReceiver to the Domain
-
Add the GroovyScriptInlineReceiver to the Domain (Click here to see instructions).

-
Configure the loop parameters. GenRocket replaces {var1} and {var2} with the Attribute values generated for the current row. Because the script uses row-level values, enable whileLoop and turn off the other loop options.
- beforeLoop = false
- whileLoop = true
- afterLoop = false

Step 3 - Select Data Tab and Enter the Inline Script
def csvFile = new File("var1" + File.separator + "var2")
def recordCount = csvFile.readLines().size() - 1
csvFile.append(
System.lineSeparator() + "TOTAL_RECORDS=${recordCount}"
)
println "Added record count to ${csvFile.name}"

Step 4 - Configure Variables in the Attributes Property Keys Tab
- Select the path Attribute and select var1 for the variable.
-
Select the subPath Attribute and select var2 for the variable.

Step 5 - Run the Scenario
If you are not using G-Repository, remember to download the Scenario. Then use the genrocket -r command to run it from the command line and add the count to your file. For this example, the command is:
genrocket -r ScriptScenario.grs
Sample Output

Example 2 - Add Record Count to Multiple CSV Files

Note
Please note that all the steps for this example are the same as Example 1, except for the subPath ConstantGen value and the Inline Groovy Script.
subPath Attribute ConstantGen Value

Groovy Inline Script
The GroovyScriptInlineReceiver configuration for this example is the same as Example 1 above. Still, it uses a different script designed for multiple files of the same type in a specific directory (shown below).
def path = "var1".replace('|', File.separator)
def folder = new File(path + File.separator + "var2")
folder.eachFile { csvFile ->
if (csvFile.isFile() && csvFile.name.endsWith(".csv")) {
def recordCount = csvFile.readLines().size() - 1
csvFile.append(
System.lineSeparator() + "TOTAL_RECORDS=${recordCount}"
)
println "Added record count to ${csvFile.name}"
}
}

Sample Output
sampleFile1

sampleFile2

sampleFile3

sampleFile4

Note
The above examples show how to get the record count and add it to the end of one or more CSV files. This is only one use case for the GroovyScriptInlineReceiver. You can use a different inline script or set up related Domains that generate the data files, which the script can then use.
Article Feedback: Was this helpful?
Give feedback