> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sample 3. Step 25: Detect the TotalQuantity and TotalAmount fields with TotalQuantity and TotalAmount elements

> Recover the Total Quantity and Total Amount values with TotalQuantity and TotalAmount Character String elements inside an optional grTotal group.

Analysis of the test images shows that the Total Quantity and Total Amount fields occur only together with the `Footer.kwTotal` name and on the same level.

Use elements of type [Character String](/flexi-capture/fls/template/character-chain) to detect the Total Quantity and Total Amount fields. To set the search constraints common to both, create a [Group](/flexi-capture/fls/template/elements-compound) element named **grTotal**.

## Create the grTotal group element

<Steps>
  <Step title="Create the Group element">
    In the **Footer** element, create a [Group](/flexi-capture/fls/template/elements-compound) element and name it **grTotal**.
  </Step>

  <Step title="Make the group optional">
    Select **Optional element** so that the search for subelements stops if the name is absent. For more information, see [The Optional property of a Group element](/flexi-capture/fls/tips-tricks/date11).
  </Step>

  <Step title="Add the search constraint">
    Click the **Advanced** tab. The **Total Quantity** and **Total Amount** fields occur only together with the `Footer.kwTotal` name, so set this [additional search constraint](/flexi-capture/fls/template/advanced-constraints) in the **Advanced pre-search relations** field: if the `Footer.kwTotal` element was not detected, do not look for the object. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):

    ```text theme={null}
    If Footer.kwTotal.IsNull Then DontFind;
    ```
  </Step>
</Steps>

## Create the TotalQuantity element

The Total Quantity field sits at the intersection of the Total row and the Quantity column. You already set the first constraint at the [Group](/flexi-capture/fls/template/elements-compound) element level. To set the second constraint, use the previously detected column name as a reference element.

<Steps>
  <Step title="Create the Character String element">
    In the **grTotal** element, create an element of type [Character String](/flexi-capture/fls/template/character-chain) and name it **TotalQuantity**.
  </Step>

  <Step title="Set the character count">
    Click the **Character String** tab. In the **Character count** field, specify this fuzzy interval: `{-1, 1, 10, INF}`. This estimates the length of the character string; assume values range from 1 to 10. Any hypothesis outside this range is penalized.

    Leave the default values for the element's other properties.
  </Step>

  <Step title="Set the position constraints">
    Click the **Relations** tab. The **Total Quantity** field always sits on the same level as the `Footer.kwTotal` name keywords or slightly lower, so set these search constraints:

    * **Above** the `Footer.kwTotal` element, **Offset** = -200, **Reference boundary** = **Bottom**
    * **Below** the `Footer.kwTotal` element, **Offset** = -20, **Reference boundary** = **Top**

    <Tip>
      When selecting offset values, analyze the geometrical properties (size and boundaries) of the neighboring objects detected during pre-recognition. Otherwise, select these values by trial and error.
    </Tip>
  </Step>

  <Step title="Add the column constraint">
    Click the **Advanced** tab. The **Total Quantity** field is optional, but whenever it occurs it appears together with the **Quantity** column and below its name. Set this [additional search constraint](/flexi-capture/fls/template/advanced-constraints) in the **Advanced pre-search relations** field: look for the object only if the `TableHeader.kwQuantity` element was detected, and look no farther than 50 dots to the left or right of the column name's respective boundaries. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):

    ```text theme={null}
    If Not (TableHeader.kwQuantity.IsNull) Then
    {
    LeftOf: TableHeader.kwQuantity.Right, -50 * dot;
    RightOf: TableHeader.kwQuantity.Left, -50 * dot;
    }
    Else DontFind;
    ```
  </Step>

  <Step title="Verify the match">
    Match the FlexiLayout with the test images and confirm that FlexiLayout Studio detects the element on every image where it occurs.
  </Step>

  <Step title="Set the block source element">
    To describe the location of the **TotalQuantity** block, select **Source element**, click <img src="https://mintcdn.com/abbyy/fmgRWFNHKYN2MLSg/images/flexi-capture/fls/Browse_Button.gif?s=3673db30dda4a3293aec716853588b55" alt="Browse button" style={{display:"inline-block",verticalAlign:"middle",margin:0}} width="21" height="20" data-path="images/flexi-capture/fls/Browse_Button.gif" />, and select the **TotalQuantity** element as the source element.
  </Step>
</Steps>

## Create the TotalAmount element

The Total Amount field sits at the intersection of the Total row (the footer of the table) and the Total column. You already set the first constraint at the [Group](/flexi-capture/fls/template/elements-compound) element level. To set the second constraint, use the previously detected column names as reference elements.

<Steps>
  <Step title="Create the Character String element">
    In the **grTotal** element, create an element of type [Character String](/flexi-capture/fls/template/character-chain) and name it **TotalAmount**.
  </Step>

  <Step title="Set the character count">
    Click the **Character String** tab. In the **Character count** field, specify this fuzzy interval: `[-1, 1, 20, 2147483647]`. This estimates the length of the character string; assume values range from 1 to 20. Any hypothesis outside this range is penalized.

    Leave the default values for the element's other properties.
  </Step>

  <Step title="Set the position constraints">
    Click the **Relations** tab. The **Total Amount** field always sits on the same level as the `Footer.kwTotal` name keywords, so set these search constraints:

    * **Above** the `Footer.kwTotal` element, **Offset** = -20, **Reference boundary** = **Bottom**
    * **Below** the `Footer.kwTotal` element, **Offset** = -20, **Reference boundary** = **Top**
  </Step>

  <Step title="Add the column constraint">
    Click the **Advanced** tab. The **Total Amount** field appears immediately below the name of the **Total** column (possibly with a small shift), between the **Unit Price** and **Sales** columns. In the **Advanced pre-search relations** field, set an [additional search constraint](/flexi-capture/fls/template/advanced-constraints) that covers two cases:

    * If `TableHeader.kwTotal` was detected, look for the object no more than 70 dots right of its right boundary and no more than 50 dots left of its left boundary.
    * Otherwise, if `TableHeader.kwUnitPrice` and `TableHeader.kwSales` were detected, look for the object no more than 40 dots right of the left boundary of `TableHeader.kwSales`, and right of the right boundary of `TableHeader.kwUnitPrice`. If neither case applies, do not look for the object.

    In the [FlexiLayout language](/flexi-capture/fls/code/general-code):

    ```text theme={null}
    If Not (TableHeader.kwTotal.IsNull) Then
    {
    LeftOf: TableHeader.kwTotal.Right, -70 * dot;
    RightOf: TableHeader.kwTotal.Left, -50 * dot;
    }
    Else
    If Not (TableHeader.kwUnitPrice.IsNull) and Not (TableHeader.kwSales.IsNull) Then
    {
    LeftOf: TableHeader.kwSales.Left, -40 * dot;
    RightOf: TableHeader.kwUnitPrice, 0 * dot;
    }
    Else DontFind;
    ```

    <Tip>
      When selecting offset values, analyze the geometrical properties (size and boundaries) of the neighboring objects detected during pre-recognition. Otherwise, select these values by trial and error.
    </Tip>
  </Step>

  <Step title="Verify the match">
    Match the FlexiLayout with the test images and confirm that FlexiLayout Studio detects the element on every image where it occurs.
  </Step>

  <Step title="Set the block source element">
    To describe the location of the **TotalAmount** block, select **Source element**, click <img src="https://mintcdn.com/abbyy/fmgRWFNHKYN2MLSg/images/flexi-capture/fls/Browse_Button.gif?s=3673db30dda4a3293aec716853588b55" alt="Browse button" style={{display:"inline-block",verticalAlign:"middle",margin:0}} width="21" height="20" data-path="images/flexi-capture/fls/Browse_Button.gif" />, and select the **TotalAmount** element as the source element.
  </Step>
</Steps>
