Iterating Basket Items with Beanshell Scripts
Overview
This tutorial will guide you through iterating the transaction basket and updating the additional data using beanshell scripts.
What you will learn
How to iterate and add additional data to basket items using BeanShell Script action.
Pre-requisites
Should have followed Training Workspace Setup and have a working POS.
Should have followed the Create new beanshell script action - Tutorial and should have the knowledge to override an existing process and add a new BeanShell Script action.
Exercises
Perform a Transaction
From this, we are going to verify the data in the Transaction.xml of a performed transaction before manipulating.
Log in to the POS.
Add Product 1 to the basket. (Type 1 on the input box and click enter)
Click on the total button and select Cash from the tender options.
Select OK.
Close Cash Drawer from the external Cash Drawer window and you will navigate back to the empty transaction basket page.
To validate the transaction basket details, we will check the Transaction.xml of the above transaction.
For this, Navigate to your EnactorHome -> pos -> Data -> Enactor Pos -> RetailTransaction -> POS externally from the file system and open the latest xml.
Observe that your basket item similar to below.
Transaction.xml
<retail:merchandiseItem isReturn="false">
<retail:lineNumber>1</retail:lineNumber>
<retail:description>KAT VON D Eye Liner</retail:description>
<retail:deviceId>pos1@0001.enactor</retail:deviceId>
<retail:value>1800</retail:value>
<retail:netValue>1800</retail:netValue>
<retail:effectiveNetValue>1500</retail:effectiveNetValue>
<retail:supplementaryValues/>
<retail:dateTimeCreated>2025-06-17T02:10:49+05:30</retail:dateTimeCreated>
<retail:userId>1010</retail:userId>
<retail:modifiers>
<retail:distributedPromotionSavingItem>
<retail:modifierType>PROMOTION</retail:modifierType>
<retail:effectiveValue>-300</retail:effectiveValue>
<retail:lineNumber>1</retail:lineNumber>
<retail:distributedSaving>300</retail:distributedSaving>
<retail:promotionKey>
<retail:promotionId>AmountDiscount</retail:promotionId>
<retail:groupKey groupHierarchyId="All" groupTypeId="region">UK</retail:groupKey>
</retail:promotionKey>
</retail:distributedPromotionSavingItem>
</retail:modifiers>
<retail:modifiersNetValue>0</retail:modifiersNetValue>
<retail:modifiersEffectiveNetValue>-300</retail:modifiersEffectiveNetValue>
<retail:modifiersVoidQuantity>0.0</retail:modifiersVoidQuantity>
<retail:modifiersQuantity>0.0</retail:modifiersQuantity>
<retail:notDiscountable>false</retail:notDiscountable>
<retail:maxDiscount>0.0</retail:maxDiscount>
<retail:productID>1</retail:productID>
<retail:productTypeId>merchandiseProduct</retail:productTypeId>
<retail:unitPrice>1800</retail:unitPrice>
<retail:quantity>1.0</retail:quantity>
<retail:netQuantity>1.0</retail:netQuantity>
<retail:handKeyed>true</retail:handKeyed>
<retail:sourceInventoryType>AVA</retail:sourceInventoryType>
<retail:taxGroupId>P</retail:taxGroupId>
<retail:tax>
<retail:taxAmount>
<retail:taxRateId>UKR</retail:taxRateId>
<retail:taxRateKey>
<retail:taxRateId>UKR</retail:taxRateId>
<retail:effectiveDate>2014-05-07T04:30:00+05:30</retail:effectiveDate>
</retail:taxRateKey>
<retail:displayTaxCode>VAT</retail:displayTaxCode>
<retail:taxAmount>3000000</retail:taxAmount>
<retail:scale>4</retail:scale>
<retail:taxRatePercentage>0.2</retail:taxRatePercentage>
<retail:taxableAmount>1500</retail:taxableAmount>
</retail:taxAmount>
</retail:tax>
<retail:orderable>true</retail:orderable>
<retail:roundedTaxAmount>300</retail:roundedTaxAmount>
<retail:priceTypeId>R</retail:priceTypeId>
<retail:productImageURL>image://PRODUCT/1177567.jpg</retail:productImageURL>
<retail:type>VALUE</retail:type>
<retail:mmGroupId>EYELINER</retail:mmGroupId>
<retail:mmGroupHierarchyId>COSMETICS</retail:mmGroupHierarchyId>
<retail:warrantyInformation></retail:warrantyInformation>
<retail:localisedWarrantyInformation/>
<retail:brandGroupId>KAT_VON_D</retail:brandGroupId>
<retail:brandGroupHierarchyId>COSMETICS</retail:brandGroupHierarchyId>
<retail:mmGroupDescription>Eyeliner</retail:mmGroupDescription>
<retail:mmGroupVariantGroupId>All</retail:mmGroupVariantGroupId>
<retail:mmGroupVariantHierarchyId>All</retail:mmGroupVariantHierarchyId>
</retail:merchandiseItem>
Adding a new Beanshell Script Action
Open the EndTransaction_1.0.xml process created from Create new BeanShell Script action - Tutorial.
Add a new BeanShell Script action by following the steps in Adding a new BeanShell Script action
In the properties tab, give "IterateBasketItems" as the Action ID.
Here, as we are going to going to iterate and update basket items, IterateBasketItems Beanshell Script action should be added after the basket has been populated in the process. Therefore we need to insert the new action in between GetTransactionDataFromHandlerAction and RetailSaleTest actions.
Since the basket is being manipulated, it should be added as both an input and output for the IterateBasketItems BeanShell Script action. To do this, double-click the required section, select "enactor.mfc.Basket", and click OK.
Double click on the Outcome section and add "Success" as an outcome to the beanshell script action.
It is important to reconnect the incoming and outgoing links of the IterateBasketItems action to maintain correct process functionality.
Therefore click on Link from palette and connect the Success outcome of IterateBasketItems action to the RetailSaleTest action.
Also, change the input link which is connected to RetailSaleTest action from GetTransactionDataFromHandlerAction action, by dragging the endpoint of the link from the RetailSaleTest action to IterateBasketItems beanshell script action.
Finally the process diagram should look as below.
Adding the Script
In this tutorial, we will retrieve the basket items and iterate through them. If any Merchandise item is found, let's add a simple additional data entry to additional data map.
Here, the Outcome should also be added to the script.
import java.util.List;
import com.enactor.mfc.basket.items.IMerchandiseItem;
if (basket != null) {
List items = basket.getItemsList();
for (int i = 0; i < items.size(); i++) {
Object item = items.get(i);
if (item != null && item instanceof IMerchandiseItem) {
item.addAdditionalDatum("beanshell - key", "beanshell - value");
}
}
}
outcome = "Success";
Copy and paste the above script on the script editor of the IterateBasketItems beanshell script action and click OK.
Save the changes.
Validate the manipultated data using a Transaction
Perform a transaction and open its Transacation.xml file by following the same steps done in Perform a Transaction.
To verify that the inserted additional data are there in the file only for Merchandise items, observe that your basket item similar to below.
Transaction.xml
<retail:merchandiseItem isReturn="false">
<retail:lineNumber>1</retail:lineNumber>
<retail:description>KAT VON D Eye Liner</retail:description>
<retail:deviceId>pos1@0001.enactor</retail:deviceId>
<retail:value>1800</retail:value>
<retail:netValue>1800</retail:netValue>
<retail:effectiveNetValue>1500</retail:effectiveNetValue>
<retail:supplementaryValues/>
<retail:dateTimeCreated>2025-06-17T01:14:29+05:30</retail:dateTimeCreated>
<retail:userId>1010</retail:userId>
<retail:additionalData>
<beanshell - key>beanshell - value</beanshell - key>
</retail:additionalData>
<retail:modifiers>
<retail:distributedPromotionSavingItem>
<retail:modifierType>PROMOTION</retail:modifierType>
<retail:effectiveValue>-300</retail:effectiveValue>
<retail:lineNumber>1</retail:lineNumber>
<retail:distributedSaving>300</retail:distributedSaving>
<retail:promotionKey>
<retail:promotionId>AmountDiscount</retail:promotionId>
<retail:groupKey groupHierarchyId="All" groupTypeId="region">UK</retail:groupKey>
</retail:promotionKey>
</retail:distributedPromotionSavingItem>
</retail:modifiers>
<retail:modifiersNetValue>0</retail:modifiersNetValue>
<retail:modifiersEffectiveNetValue>-300</retail:modifiersEffectiveNetValue>
<retail:modifiersVoidQuantity>0.0</retail:modifiersVoidQuantity>
<retail:modifiersQuantity>0.0</retail:modifiersQuantity>
<retail:notDiscountable>false</retail:notDiscountable>
<retail:maxDiscount>0.0</retail:maxDiscount>
<retail:productID>1</retail:productID>
<retail:productTypeId>merchandiseProduct</retail:productTypeId>
<retail:unitPrice>1800</retail:unitPrice>
<retail:quantity>1.0</retail:quantity>
<retail:netQuantity>1.0</retail:netQuantity>
<retail:handKeyed>true</retail:handKeyed>
<retail:sourceInventoryType>AVA</retail:sourceInventoryType>
<retail:taxGroupId>P</retail:taxGroupId>
<retail:tax>
<retail:taxAmount>
<retail:taxRateId>UKR</retail:taxRateId>
<retail:taxRateKey>
<retail:taxRateId>UKR</retail:taxRateId>
<retail:effectiveDate>2014-05-07T04:30:00+05:30</retail:effectiveDate>
</retail:taxRateKey>
<retail:displayTaxCode>VAT</retail:displayTaxCode>
<retail:taxAmount>3000000</retail:taxAmount>
<retail:scale>4</retail:scale>
<retail:taxRatePercentage>0.2</retail:taxRatePercentage>
<retail:taxableAmount>1500</retail:taxableAmount>
</retail:taxAmount>
</retail:tax>
<retail:orderable>true</retail:orderable>
<retail:roundedTaxAmount>300</retail:roundedTaxAmount>
<retail:priceTypeId>R</retail:priceTypeId>
<retail:productImageURL>image://PRODUCT/1177567.jpg</retail:productImageURL>
<retail:type>VALUE</retail:type>
<retail:mmGroupId>EYELINER</retail:mmGroupId>
<retail:mmGroupHierarchyId>COSMETICS</retail:mmGroupHierarchyId>
<retail:warrantyInformation></retail:warrantyInformation>
<retail:localisedWarrantyInformation/>
<retail:brandGroupId>KAT_VON_D</retail:brandGroupId>
<retail:brandGroupHierarchyId>COSMETICS</retail:brandGroupHierarchyId>
<retail:mmGroupDescription>Eyeliner</retail:mmGroupDescription>
<retail:mmGroupVariantGroupId>All</retail:mmGroupVariantGroupId>
<retail:mmGroupVariantHierarchyId>All</retail:mmGroupVariantHierarchyId>
</retail:merchandiseItem>