> ## 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.

# Creating a set of functions for processing and verifying multiple documents

> Create the Pega functions for multi-document capture: CreateNewBatch, AddDocumentToBatch, RunBatchProcessing, and FetchingCapturedData.

To implement a multi-document processing scenario, create the following functions:

[CreateNewBatch](#createnewbatch)

[AddDocumentToBatch](#adddocumenttobatch)

[RunBatchProcessing](#runbatchprocessing)

[WaitFirstVerificationOrProcessedForMultiFileInvoice](#waitfirstverificationorprocessedformultifileinvoice)

[FetchingCapturedData](#fetchingcaptureddata)

[MultiDocumentBatchIsProcessed](#multidocumentbatchisprocessed)

## <a id="createnewbatch" />CreateNewBatch

The *CreateNewBatch* function creates a new batch and returns its identifier.

There are no input parameters for this function.

**Output parameter type**: *int* (batch identifier).

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Creating FlexiCapture batch and document.
    Batch batch = new Batch();
    Document doc = new Document();
    // Adding batch into FlexiCapture project.
    return projApi.getBatches().add(batch);
  }
} catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```

## <a id="adddocumenttobatch" />AddDocumentToBatch

The *AddDocumentToBatch* function adds a document to a batch.

**Input parameters**:

| **Name**      | **Java type** | **Description**                |
| ------------- | ------------- | ------------------------------ |
| batchId       | int           | Batch identifier               |
| fileName      | String        | Name of the file               |
| base64Content | String        | File contents in Base64 format |

There are no output parameters for this function.

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Creating FlexiCapture document.
    Document doc = new Document();
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Creating instance of file. 
    File file = new File(fileName, Base64.getDecoder().decode(base64Content.replace("\n", "").replace("\r", "")));
    // Adding document into FlexiCapture batch.
    // File is document content.
    batchApi.getDocuments().add(doc, file);
  }
} catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```

## <a id="runbatchprocessing" />RunBatchProcessing

The *RunBatchProcessing* function initializes batch processing.

**Input parameters**:

| **Name** | **Java type** | **Description**  |
| -------- | ------------- | ---------------- |
| batchId  | int           | Batch identifier |

There are no output parameters for this function.

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Running the FlexiCapture batch for processing.
    batchApi.start();
  }
} catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```

## <a id="waitfirstverificationorprocessedformultifileinvoice" />WaitFirstVerificationOrProcessedForMultiFileInvoice

The *WaitFirstVerificationOrProcessedForMultiFileInvoice* function returns a verification URL. If the document has not reached the verification stage, an empty string is returned.

**Input parameters**:

| **Name** | **Java type** | **Description**  |
| -------- | ------------- | ---------------- |
| batchId  | int           | Batch identifier |

**Output parameter type**: *String* (verification URL).

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Waiting for the batch to be stopped on the verification stage and getting the FlexiCapture document.
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // Check if the batch stage is Verification.
    if (doc.getStageType() == ProcessingStageType.Verification) {
      // Returning first verification url string for FlexiCapture batch.
      return batchApi.getVerificationUrls(WebPageMode.Mini).get(0).toString();
    } 
    //Returning empty string if batch was processed without verification stage.
    return "";
  }
}catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```

## <a id="fetchingcaptureddata" />FetchingCapturedData

The *FetchingCapturedData* function returns a list of output fields in JSON format.

**Input parameters**:

| **Name**     | **Java type** | **Description**          |
| ------------ | ------------- | ------------------------ |
| batchId      | int           | Batch identifier         |
| templateName | String        | Document Definition name |

**Output parameter type**: *String* (document fields in JSON format).

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Getting all documents from FlexiCapture batch.
    List<Document> documents = batchApi.getDocuments().getAll();
    for(Document document:
       documents){
      // Cheking document template name.
      if(document.getTemplateName().equals(templateName)){
        // Getting the FlexiCapture document api. Please provide a document identifier here.
        DocumentApi docApi = batchApi.getDocuments().getDocumentApi(document.getId());
        // Returning captured fields as a JSON string.
        return docApi.getExportedFields();
      }
    }
    //Returning empty JSON string if document template name not found.
    return "{}";
  }
}catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```

## <a id="multidocumentbatchisprocessed" />MultiDocumentBatchIsProcessed

The *MultiDocumentBatchIsProcessed* function returns a value that specifies whether the batch has been processed or not.

**Input parameters**:

| **Name** | **Java type** | **Description**  |
| -------- | ------------- | ---------------- |
| batchId  | int           | Batch identifier |

**Output parameter type**: *boolean* (*true* if the document is at the *Processed* stage and *false* otherwise).

**Exceptions thrown**: Exception.

**Java source**:

```
// Creating FlexiCapture Web Services API client. 
// Please provide FlexiCapture Application Server address, tenant name, user name and password here. 
try (TenantClient tenantClient = new TenantClient(new URI(baseUri), tenantName, userName, password)) {
  // Getting the FlexiCapture project api. Please provide a project name here.
  try(ProjectApi projApi = tenantClient.getProjects().getProjectApi(multiFileProjectName)) {
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Waiting for the batch to be stopped on the verification or processed stage and getting the FlexiCapture document.
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // Check if the batch stage is Processed.
    return doc.getStageType() == ProcessingStageType.Processed;    
  }
}catch(Exception ex){
  // Chaining error messages. 
Throwable cause = ex.getCause();
String message = ex.getMessage();
while (cause != null) {
message += " " + cause.getMessage();
cause = cause.getCause();
}
// Throw chained message.
throw new Exception(message);
}
```
