Skip to content

Tick a Line Flag on Every Line That Shares a Reference ​

This Groovy script keeps a yes/no field consistent across the lines of a miscellaneous invoice. The user ticks the field on one line; when the invoice is saved, every line with the same reference gets it ticked too, and every other line gets it unticked.

Recommended pattern

This script is not tied to any customer's setup. Both the yes/no field and the reference field are passed in as field ids, so the same script works for any pair of line fields.

Class nameEAGroovyUpdateRefInMiscPurchaseInvDetails
ScriptEAGroovyUpdateRefInMiscPurchaseInvDetails.groovy
Runs onMiscellaneous Invoice, on Update Calculated Fields
UsesParameter 2 and Parameter 3. Leave Parameter 1 empty.
Shipped asSource to paste. It is not built into the system.

When to use it ​

A miscellaneous invoice often carries several lines for the same thing: three lines for the same purchase element, or several lines for the same project in Ref 1. When a flag on the line has to mean "this group", for example include in the monthly recharge, ticking it line by line is slow and easy to get half right. With this script the user ticks it on one line of the group, and the save does the rest.

The script is also a small example of a pattern that is useful in many Groovy flows: the fields it works on are not written into the code but read from the parameters, so one script serves many setups.

How it works ​

  1. It finds the first ticked line. It goes through the lines in order and stops at the first one whose yes/no field (Parameter 2) is ticked. If no line is ticked, nothing changes.
  2. It takes that line's reference. It reads the field named in Parameter 3 on that line.
  3. It sets the flag on every line. Each line whose Parameter 3 field holds the same value is ticked. Every other line is unticked, including lines the user had ticked themselves.

Only the first ticked line counts. If the user ticks lines from two different groups, the group of the upper line wins and the other group is unticked.

Setting it up ​

  1. Open Administration → Display Customization → Entity Flow and create a new flow with Target Type set to Miscellaneous Invoice.
  2. Add a line to the Details grid with Target Action set to Update Calculated Fields. Paste the whole script from the file above into Groovy Script, and leave Class Name empty: it is filled in when you save.
  3. Fill in the parameters. Both take the field id of a line field, not its label:
ParameterTitle shown after savingWhat to enter
Parameter 1Not used - leave emptyNothing.
Parameter 2Yes/No line field id - eg: b1The yes/no line field to keep consistent, for example b1 for the line's first yes/no field.
Parameter 3Reference line field id - eg: ref1The line field that defines the group, for example ref1, or purchaseElement for the purchase element.
  1. Save the flow. Class Name shows the script's class name, the description appears in Description, and each parameter shows its title. Saving is refused with Parameter {0} can not be empty when Parameter 2 or Parameter 3 is empty.

Example ​

With Parameter 2 set to b1 and Parameter 3 set to purchaseElement, the user enters these lines and ticks b1 on lines 2 and 3:

LinePurchase elementb1 as enteredb1 after saving
1Cleaning
2Security✓✓
3Cleaning✓
4Security✓

Line 2 is the first ticked line, so the group is Security. Line 4 is ticked because it is Security too, and line 3 is unticked because it is not.

The script ​

groovy
package com.namasoft.modules.accounting.domain.utils.actions

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.accounting.domain.details.MiscPurchaseInvLine
import com.namasoft.modules.accounting.domain.entities.MiscPurchaseInvoice
import com.namasoft.modules.basic.domain.primitives.EntityTargetAction
import com.namasoft.specialserialization.ReflectionUtils

public class EAGroovyUpdateRefInMiscPurchaseInvDetails implements EntityAction<MiscPurchaseInvoice> {

    @Override
    Result doAction(MiscPurchaseInvoice invoice, LongTextDF... parameters) {
        Result result = Result.createAccumulatingResult();
        MiscPurchaseInvLine firstMatchedLine = invoice.getDetails().stream().filter { l -> ObjectChecker.areEqual(ReflectionUtils.getFieldValue(parameters[1].getPrimitiveValue(), l), BooleanDF.TRUE()) }.findFirst()
                .orElse(null);
        if (firstMatchedLine == null)
            return result;
        for (MiscPurchaseInvLine line in invoice.getDetails()) {
            if (ObjectChecker.areEqual(ReflectionUtils.getFieldValue(parameters[2].getPrimitiveValue(), line), ReflectionUtils.getFieldValue(parameters[2].getPrimitiveValue(), firstMatchedLine)))
                ReflectionUtils.setProperty(line, parameters[1].getPrimitiveValue(), BooleanDF.TRUE());
            else
                ReflectionUtils.setProperty(line, parameters[1].getPrimitiveValue(), BooleanDF.FALSE());
        }
        return result;
    }

    @Override
    Result validateParameters(ParamValidationInfo info, EntityTargetAction action, LongTextDF... parameters) {
        Result result = Result.createAccumulatingResult();
        EntityAction.ensureParamIsNotEmpty(info, result, 1, parameters);
        EntityAction.ensureParamIsNotEmpty(info, result, 2, parameters);
        return result;
    }

    @Override
    String describe() {
        return "Miscellaneous invoice: finds the first line whose yes/no field (field id in parameter 2, eg: b1) is ticked. That field is then ticked on " +
                "every line whose reference field (field id in parameter 3, eg: ref1) holds the same value as that line, and unticked on all the other lines. " +
                "Nothing changes when no line is ticked. Parameter 1 is not used."
    }

    @Override
    List<String> columnNames() {
        return Arrays.asList("Not used - leave empty", "Yes/No line field id - eg: b1", "Reference line field id - eg: ref1")
    }

}