> ## 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 assembly scripts

> Sample assembly scripts for ABBYY FlexiCapture document sets: one adds required documents based on a loan sum, another checks for missing documents.

These sample scripts assemble documents into document sets defined by a Document Set Definition and report assembly problems through the `AssemblingErrors` object. The first script requires different documents depending on a loan sum. The second checks that a set contains the expected identification documents.

## Require documents based on the loan sum

This script assembles documents into document sets defined by a Document Set Definition. The Document Set Definition contains a section named **Borrower's form** and references to the following Document Definitions:

* Passport
* Guarantor's documents
* Co-borrower's documents
* Employer reference

The **Borrower's form** section includes the following fields:

* Co-borrower's name
* Loan sum

The documents that the document set must contain depend on the value of the **Loan sum** field. The script uses two threshold values to determine which documents are required:

| Loan sum                                                        | Required documents                                           |
| --------------------------------------------------------------- | ------------------------------------------------------------ |
| Less than the first threshold value                             | Passport                                                     |
| Greater than the first threshold value but less than the second | Passport and an employer reference                           |
| Greater than the second threshold value                         | Passport, an employer reference, and a guarantor's documents |

If the **Co-borrower's name** field contains a value, the document set must also contain a co-borrower's documents.

The following script uses this logic to assemble document sets:

```csharp theme={null}
using System.Collections.Generic;
int creditSum = 0;
bool creditSumFound = false;
string cocreditor = "";
int formCount = 0;
string formName = "Borrower's form";
// Document Definitions used in the document set
int referenceFromWorkCount = 0;
string referenceFromWorkName = "Employer reference";
int guaranteeDocsCount = 0;
string guaranteeDocsName = "Guarantor's documents";
int cocreditorDocsCount = 0;
string cocreditorDocsName = "Co-borrower's documents";
int passportCount = 0;
string passportName = "Passport";
foreach( IBatchItem item in BatchItems ) {
    // Find the form and determine the sum of the loan
    if (item.Type == TBatchItemType.BIT_Page)
    {
        if (item.AsPage.SectionName != formName)
            AssemblingErrors.AddCustomError("Cannot contain the following sections: " + item.AsPage.SectionName, 1);
        else
        {
            cocreditor = item.AsPage.Document.IndexedItemValue("Borrower's name").ToString();
            string creditSumText = item.AsPage.Document.IndexedItemValue("Loan sum").ToString();
            if (!int.TryParse(creditSumText, out creditSum))
                AssemblingErrors.AddCustomError("Loan sum was not recognized: " + creditSumText, 1);
            else
                creditSumFound = true;
            formCount++;
        }
    }
    // Count the documents in the set
    else if (item.Type == TBatchItemType.BIT_Document) {
        if (item.AsDocument.TemplateName == referenceFromWorkName)
            referenceFromWorkCount++;
        else if (item.AsDocument.TemplateName == guaranteeDocsName)
            guaranteeDocsCount++;
        else if (item.AsDocument.TemplateName == passportName)
            passportCount++;
        else if (item.AsDocument.TemplateName == cocreditorDocsName)
            cocreditorDocsCount++;
        else
            AssemblingErrors.AddCustomError("The document set cannot contain the document " + item.AsDocument.TemplateName, 1);
    }
}
// Check the number of sections and documents in the set
if (formCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + formName, formCount);
else if (formCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + formName, 1);
if (passportCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + passportName, passportCount);
else if (passportCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + passportName, 1);
// Check that the document set contains a co-borrower if a co-borrower name is present in the form
if (cocreditor != "" && cocreditorDocsCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + cocreditorDocsName, 1);
else if (cocreditor != "" && cocreditorDocsCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + cocreditorDocsName, cocreditorDocsCount);
else if (cocreditor == "" && cocreditorDocsCount > 0)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + cocreditorDocsName, cocreditorDocsCount);
// Check that the document set contains all of the document required due to the loan sum exceeding one of the thresholds
if (creditSumFound)
{
    if (creditSum > 50000) // requires an employer reference
    {
        if (referenceFromWorkCount > 1)
            AssemblingErrors.AddCustomError("There are too many documents of the type: " + referenceFromWorkName, referenceFromWorkCount);
        else if (referenceFromWorkCount < 1)
            AssemblingErrors.AddCustomError("Missing the following documents: " + referenceFromWorkName, 1);
    }
    if (creditSum > 500000) // requires the guarantor's documents
    {
        if (guaranteeDocsCount > 1)
            AssemblingErrors.AddCustomError("There are too many documents of the type: " + guaranteeDocsName, guaranteeDocsCount);
        else if (guaranteeDocsCount < 1)
            AssemblingErrors.AddCustomError("Missing the following documents: " + guaranteeDocsName, 1);
    }
}
```

## Check for missing or duplicate documents

This script works with the **Identification Documents** Document Set Definition, which references the following Document Definitions:

* Passport
* Driver's license

A document set may contain at most one document of each type, and must contain at least one of these documents.

The following script uses this logic to assemble document sets:

```csharp theme={null}
using System.Collections.Generic;
using System;
string docSetName = "Identification Documents";
int pagesCnt = 0;
// List of the types documents that may be included in the set
List<string> allowed = new List<string> {"Passport", "Driver's license"};
// Limit on the number of documents of each type in the set
List<int> allowedCount = new List<int>{1,1};
// The number of documents detected in the set
List<int> foundCount = new List<int>{0,0};
// Count the number of documents and pages in the set
foreach (IBatchItem item in BatchItems) {
    if (item.Type == TBatchItemType.BIT_Document) {
        if (item.AsDocument.TemplateName == docSetName)
            AssemblingErrors.AddCustomError("Cannot contain a nested set", 1);
        else if (allowed.Contains(item.AsDocument.TemplateName)) {
            int i = allowed.IndexOf(item.AsDocument.TemplateName);
            foundCount[i]++;
        }
    }
    else if (item.Type == TBatchItemType.BIT_Page) pagesCnt++;
}
// Check the number of documents and pages
if (foundCount[0] + foundCount[1] == 0)
    AssemblingErrors.AddCustomError("At least one identification document is required", 1);
for (int i = 0; i < allowed.Count; i++) {
    if (foundCount[i] > allowedCount[i])
        AssemblingErrors.AddCustomError("Maximum number of documents: " + allowed[i] + ": " + allowedCount[i]);
}
if (pagesCnt > 0) AssemblingErrors.AddCustomError("The document set cannot contain pages that do not belong to a document", pagesCnt);
```
