> ## 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 a single document

> Build the Pega library functions for single-document capture: SendForProcessing, WaitFirstVerificationOrProcessed, and FetchingCapturedData.

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

[SendForProcessing](#sendforprocessing)

[WaitFirstVerificationOrProcessed](#waitfirstverificationorprocessed)

[FetchingCapturedData](#fetchingcaptureddata)

[SingleDocumentBatchIsProcessed](#singledocumentbatchisprocessed)

## <a id="sendforprocessing" />SendForProcessing

The *SendForProcessing* function sends a file to FC for recognition and returns a batch identifier.

**Input parameters**:

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

**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(singleFileProjectName)) {
    // Creating FlexiCapture batch and document.
    Batch batch = new Batch();
    Document doc = new Document();
    // Adding batch into FlexiCapture project.
    int batchId = projApi.getBatches().add(batch);
    // 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.
    int docId = batchApi.getDocuments().add(doc, file);
    // Running the FlexiCapture batch for processing.
    batchApi.start();
    //Returning batch identifier.
    return batchId;
  }
} 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="waitfirstverificationorprocessed" />WaitFirstVerificationOrProcessed

The *WaitFirstVerificationOrProcessed* 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(singleFileProjectName)) {
    // 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 |

**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(singleFileProjectName)) {
    // Getting the FlexiCapture batch api. Please provide a batch identifier here.
    BatchApi batchApi = projApi.getBatches().getBatchApi(batchId);
    // Waiting for the batch to be processed and getting the FlexiCapture document.
    Document doc = batchApi.getDocuments().waitFirstVerificationOrProcessed();
    // Getting the FlexiCapture document api. Please provide a document identifier here.
    DocumentApi docApi = batchApi.getDocuments().getDocumentApi(doc.getId());
    // Returning captured fields as a JSON string.
    return docApi.getExportedFields();
  }
}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="singledocumentbatchisprocessed" />SingleDocumentBatchIsProcessed

The *SingleDocumentBatchIsProcessed* function returns a value that specifies whether a 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(singleFileProjectName)) {
    // 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);
}
```
