Search code examples
abbyy

Calculate sum in script in ABBYY Flexicapture


I would like to perform the function of a Calculate Sum rule with a Script rule in ABBYY Flexicapture, because I want to only perform the calculation based on evaluation of an if statement.

I have tried the following in a C# script rule:

IFields AllTaxes = Context.Field("cu_location_taxes").Rows;

which gives me the error "Field is not a table."

I have also tried

IFields AllTaxes = Context.Field("cu_location_taxes").Children;

which gives me the error "Cannot access fields not specified in rule settings." Even though I have added the repeating group cu_location_taxes to the c# script rule.

Once I am able to get them in some kind of array or list or IFields variable, I would like to sum the children values in some way. I am open to doing this with JScript or C#.


Solution

  • The reasons of the errors you are facing can be found in ABBYY FlexiCapture Help. In the description of IField class you can find the following descriptions of properties:

    Rows - A set of table rows. Unavailable for non-table fields.
    Well, it seems to be that "cu_location_taxes" is not a table. You said, it is a repeating group.

    Children - Child items of the field (cells for tables). Unavailable in script rules. But as I understand, you are exactly using script rules.

    To achieve correct results try to use Items property exactly of the fields that you are summing.

    For example, you have a repeating group that contains number fields field_1 and field_2. And you want to calculate the sum of all field_1 instances. Then you can use the following code (JScript):

    sum = 0
    for (i = 0; i < this.Field("field_1").Items.Count; ++i)
    {
        sum += this.Field("field_1").Items.Item(i).Value
    }
    

    Also do not forget to add the field_1 in available fields of your rule settings. Hope this will help.