Skip to content

Clear the Locator When the Warehouse Does Not Use Locators ​

This Groovy script removes a locator from every document line whose warehouse does not use locators. It is the smallest of the supply chain examples: one loop, one check, no parameters.

Recommended pattern

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

Class nameEAClearLocatorWhenWarehouseDoesNotAllowLocators
ScriptEAClearLocatorWhenWarehouseDoesNotAllowLocators.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 ​

A company with some warehouses that use locators and some that do not runs into this when a line's warehouse is changed. The user picks a locator in a locator warehouse, then moves the line to a small store where Locator Usage is Prevented. The old locator stays on the line, even though it belongs to another warehouse and the new one does not use locators at all. The same happens with documents copied or imported from another warehouse.

With this script on the flow the leftover locator is simply dropped when the document is saved.

How it works ​

For every line that has a locator, the script looks at the line's warehouse and clears the locator when:

  • the line has no warehouse, or
  • the warehouse's Locator Usage is empty, or
  • the warehouse's Locator Usage is Prevented.

Lines in a warehouse whose Locator Usage is Required or Optional keep their locator. The script never refuses the save.

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 ​

Warehouse 01 has Locator Usage set to Prevented. A stock issue line is moved to warehouse 01 but still shows the locator A-01-03 from the warehouse it was copied from. On save, the locator is cleared, and the issue is saved without one.

The script ​

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

import com.namasoft.common.utilities.ObjectChecker
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.entities.Warehouse

public class EAClearLocatorWhenWarehouseDoesNotAllowLocators implements EntityAction<BasicSCDocument> {
    @Override
    Result doAction(BasicSCDocument doc, LongTextDF... parameters) {
        for (BasicSCDocumentLine line : doc.getDetails()) {
            if (line.getSpecificDimensions() == null || line.getSpecificDimensions().getLocator() == null)
                continue
            if (warehouseDoesNotAllowLocators(line.getSpecificDimensions().getWarehouse()))
                line.getSpecificDimensions().setLocator(null)
        }
        return Result.createAccumulatingResult()
    }

    private static boolean warehouseDoesNotAllowLocators(Warehouse warehouse) {
        if (warehouse == null)
            return true
        return ObjectChecker.isEmptyOrNull(warehouse.getLocatorPolicy()) || warehouse.getLocatorPolicy().isPrevented()
    }

    @Override
    String describe() {
        return "Clears the locator of every document line whose warehouse is empty, or whose warehouse has an empty Locator Usage " +
                "or a Locator Usage of Prevented. Lines with no locator are left as they are. Takes no parameters."
    }

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