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

# Create the functions for multi-document processing

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

A multi-document processing scenario needs six functions. Create each one as described in [Create a function](/flexi-capture/connectors/pega/pega-installation-3#func).

## Functions to create

| Function                                                                                                    | What it does                                                                    |
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| [CreateNewBatch](#createnewbatch)                                                                           | Creates a new batch and returns its identifier.                                 |
| [AddDocumentToBatch](#adddocumenttobatch)                                                                   | Adds a document to an existing batch.                                           |
| [RunBatchProcessing](#runbatchprocessing)                                                                   | Starts processing the batch.                                                    |
| [WaitFirstVerificationOrProcessedForMultiFileInvoice](#waitfirstverificationorprocessedformultifileinvoice) | Returns a verification URL, or an empty string if verification is not required. |
| [FetchingCapturedData](#fetchingcaptureddata)                                                               | Returns the output fields of one Document Definition in JSON format.            |
| [MultiDocumentBatchIsProcessed](#multidocumentbatchisprocessed)                                             | Reports whether the batch has been processed.                                   |

## CreateNewBatch

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

### Parameters

None.

### Return value

`int`, the batch identifier.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```

## AddDocumentToBatch

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

### Parameters

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

### Return value

None.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```

## RunBatchProcessing

The `RunBatchProcessing` function starts batch processing.

### Parameters

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

### Return value

None.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```

## WaitFirstVerificationOrProcessedForMultiFileInvoice

The `WaitFirstVerificationOrProcessedForMultiFileInvoice` function returns a verification URL. If the document has not reached the verification stage, it returns an empty string.

### Parameters

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

### Return value

`String`, the verification URL.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```

## FetchingCapturedData

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

### Parameters

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

### Return value

`String`, the document fields in JSON format.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```

## MultiDocumentBatchIsProcessed

The `MultiDocumentBatchIsProcessed` function reports whether the batch has been processed.

### Parameters

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

### Return value

`boolean`, which is `true` if the document is at the `Processed` stage and `false` otherwise.

### Exceptions

`Exception`

### Java source

```java theme={null}
// 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);
}
```
