Skip to content

Move a Serial Number Typed by Mistake Into the Lot ​

This Groovy script fixes a common data-entry slip on document lines: a batch number typed in the Serial Number column of an item that is tracked by lot, not by serial. The script moves the number into Lot ID and clears the serial number.

Recommended pattern

This script is not tied to any customer's setup. It has no parameters: attach it and it works.

Class nameEACopySerialToLot
ScriptEACopySerialToLot.groovy
Runs onAny supply chain document with lines. Target action Pre Update Calculated Fields.
UsesNo parameters. Leave Parameter 1 empty.
Shipped asSource to paste. It is not built into the system.

When to use it ​

Scanners, imports and users in a hurry often put the batch number in the wrong column. When the document holds both serial-tracked and lot-tracked items, the two columns sit side by side and the mistake is easy to make. The save is then refused because the item does not use serial numbers, and the lot it needs is missing.

With this script on the flow, the number is moved to where it belongs before the system checks the line.

How it works ​

The script changes a line only when all of these are true:

  • the line has an item,
  • the item has Has Lot switched on and Has Serial switched off,
  • the line's Lot ID is empty,
  • the line's Serial Number is not empty.

On such a line, the serial number becomes the Lot ID, and Serial Number is cleared. Every other line is left untouched; in particular, a line that already has a lot keeps both its lot and its serial number as they were. The script never refuses the save. It also runs when a document is saved as a draft.

Setting it up ​

  1. Open Administration → Display Customization → Entity Flow and create a flow whose target type is the document to cover (for example Stock Issue).
  2. In the Details grid, add a line with Target Action set to Pre Update Calculated Fields, and paste the whole script into its Groovy Script column. Leave Class Name empty: it is filled in when you save.
  3. Leave all parameters empty, and save the flow.

Example ​

Item MED-500 is tracked by lot only. A stock issue line for it has Serial Number B2026-0042 and no lot. After saving, the line shows Lot ID B2026-0042 and an empty serial number.

The script ​

groovy
package com.namasoft.modules.supplychain.domain.utils.plugnplay.groovy

import com.namasoft.common.utilities.ObjectChecker
import com.namasoft.infra.domainbase.datafields.BooleanDF
import com.namasoft.infra.domainbase.datafields.LongTextDF
import com.namasoft.infra.domainbase.entity.base.EntityAction
import com.namasoft.infra.domainbase.util.Result
import com.namasoft.modules.supplychain.domain.details.BasicSCDocumentLine
import com.namasoft.modules.supplychain.domain.entities.BasicSCDocument
import com.namasoft.modules.supplychain.domain.primitives.LotId
import com.namasoft.modules.supplychain.domain.primitives.SerialNumber

public class EACopySerialToLot implements EntityAction<BasicSCDocument> {
    @Override
    Result doAction(BasicSCDocument doc, LongTextDF... parameters) {
        Result result = Result.createAccumulatingResult();
        for (BasicSCDocumentLine l : doc.getDetails()) {
            if (l.fetchItem() == null)
                continue;
            if (l.getSpecificDimensions() == null)
                continue;
            if (ObjectChecker.isNotEmptyOrNull(l.getSpecificDimensions().getLotId()))
                continue;
            if (ObjectChecker.isEmptyOrNull(l.getSpecificDimensions().getSerialNumber()))
                continue;
            if (BooleanDF.isTrue(l.fetchItem().getHasSerial()))
                continue;
            if (BooleanDF.isFalse(l.fetchItem().getHasLot()))
                continue;
            l.getSpecificDimensions().setLotId(LotId.fromString(l.getSpecificDimensions().getSerialNumber().getPrimitiveValue()));
            l.getSpecificDimensions().setSerialNumber(SerialNumber.createEmpty());
        }
        return result;
    }

    @Override
    String describe() {
        return "For items that are lot tracked but not serial tracked: when a document line has a serial number and no lot, " +
                "moves the serial number into the lot and clears the serial number. Takes no parameters.";
    }

    @Override
    List<String> columnNames() {
        return Arrays.asList("Not used - leave empty");
    }
}