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

# Install the Pega connector for the FlexiCapture as a service scenario

> Install the ABBYY FlexiCapture connector for Pega by importing FlexiCaptureConnector.jar to the server and creating a Java function that calls its classes.

Installing the ABBYY FlexiCapture Connector for Pega takes two parts: importing the connector to the server, and creating a Java function in Pega that calls the connector classes.

<h2 id="import">
  Import the connector to the server
</h2>

<Steps>
  <Step title="Open the Import wizard">
    Open **Dev Studio** and click **Configure → Application → Distribution → Import**.

    <Frame>
      <img src="https://mintcdn.com/abbyy/KEmWsO92hkr7eybZ/images/flexi-capture/connectors/Pega1.png?fit=max&auto=format&n=KEmWsO92hkr7eybZ&q=85&s=a341f96a004c8a19fb49207d18680709" alt="Screenshot of Pega Dev Studio with the Configure menu open, showing the Application → Distribution → Import path being selected to import the connector." width="935" height="329" data-path="images/flexi-capture/connectors/Pega1.png" />
    </Frame>
  </Step>

  <Step title="Load the connector">
    Load the `FlexiCaptureConnector.jar` file on to the server.
  </Step>

  <Step title="Restart the server">
    Open the **Pega Self-Service Portal** and click **Restart Server**.

    <Frame>
      <img src="https://mintcdn.com/abbyy/Xgx3EG8BZs5YQjIJ/images/flexi-capture/connectors/Pega2.png?fit=max&auto=format&n=Xgx3EG8BZs5YQjIJ&q=85&s=eecde34cbec1e5bd944b2b7b9ae52933" alt="Screenshot of the Pega Self-Service Portal Operations page with the Restart Server button highlighted to restart the application server." width="537" height="297" data-path="images/flexi-capture/connectors/Pega2.png" />
    </Frame>
  </Step>
</Steps>

<h2 id="java">
  Create a Java function in Pega
</h2>

The function calls methods from the connector classes.

<Steps>
  <Step title="Create a library">
    1. Click **Records → Technical → Library → Create**.
    2. Name the library, for example `FlexiCaptureLibrary`.
    3. Include the `java.util.*`, `java.nio.file.*`, `com.abbyy.connectors.*`, and `com.abbyy.connectors.models.*` packages.

    <Frame>
      <img src="https://mintcdn.com/abbyy/Xgx3EG8BZs5YQjIJ/images/flexi-capture/connectors/Pega48.png?fit=max&auto=format&n=Xgx3EG8BZs5YQjIJ&q=85&s=c0db6b32efd6466d4e21a56900335a91" alt="Screenshot of Pega Dev Studio editing the FlexiCaptureLibrary record on the Packages tab, listing the java.util, java.nio.file, com.abbyy.connectors, and com.abbyy.connectors.models packages to include." width="577" height="394" data-path="images/flexi-capture/connectors/Pega48.png" />
    </Frame>
  </Step>

  <Step title="Generate the library">
    Click **Save**, and then click **Generate Library**.
  </Step>

  <Step title="Create the function">
    1. Click **Records → Technical → Function → Create**.
    2. In the **Label** field, provide a name for the new function, for example `CaptureData`.
  </Step>

  <Step title="Specify the input parameters">
    In the **Parameters** section, specify the following two input parameters:

    | Name            | Java type |
    | --------------- | --------- |
    | `FileName`      | `String`  |
    | `Base64Content` | `String`  |
  </Step>

  <Step title="Select the library">
    In the **Library** field, enter the name of the library you created, and then click **Create and open**.
  </Step>

  <Step title="Set the output data type">
    In the window that opens, on the **Parameters** tab, specify `String` as the data type in the **Java data type** field.

    <Frame>
      <img src="https://mintcdn.com/abbyy/Xgx3EG8BZs5YQjIJ/images/flexi-capture/connectors/Pega26.png?fit=max&auto=format&n=Xgx3EG8BZs5YQjIJ&q=85&s=ed724ead37a0bdc2a4a9a863b618b362" alt="Screenshot of Pega Dev Studio on the function Parameters tab, showing the FileName and Base64Content String input parameters and String selected as the Java data type in the Output section." width="710" height="495" data-path="images/flexi-capture/connectors/Pega26.png" />
    </Frame>
  </Step>

  <Step title="Add the function code">
    Copy the code from the `CodeExample.java` file and paste it on the **Java** tab of the same window.

    The file is in this folder:

    ```text theme={null}
    %Installation Path%\Connector for FlexiCapture as a Service\Samples\Pega Function
    ```

    The sample code is reproduced in [Sample function code](#sample-code).
  </Step>

  <Step title="Declare the thrown exception">
    Click the **Imports & Exceptions** tab and type `Exception` in the **Exceptions thrown** section.
  </Step>

  <Step title="Generate the function">
    Go back to the **Java** tab, select the **Function ready to be compiled?** option, save your changes, and then click **Generate function**.

    A successful generation shows an acknowledgement with a `Good` status.

    <Frame>
      <img src="https://mintcdn.com/abbyy/Xgx3EG8BZs5YQjIJ/images/flexi-capture/connectors/Pega3.png?fit=max&auto=format&n=Xgx3EG8BZs5YQjIJ&q=85&s=0b973102dd28000d18049bfa8285b7da" alt="Screenshot of a Pega Library Acknowledgement dialog with a Good status and the message that the Library has been generated successfully." width="364" height="282" data-path="images/flexi-capture/connectors/Pega3.png" />
    </Frame>
  </Step>
</Steps>

<h2 id="sample-code">
  Sample function code
</h2>

This code sends files to ABBYY FlexiCapture and gets the processing results back as JSON strings holding the names of the captured fields and their values.

Replace the placeholder connection values with the data required to connect to your ABBYY FlexiCapture Application Server: the server address, the company name if you use ABBYY FlexiCapture Cloud or a tenant, the user credentials, and the project name. Each one is marked by a comment in the code.

```java theme={null}
// Creating FlexiCapture Web Services API client. Please provide FlexiCapture Application Server address here. 
try (FlexiCaptureWebServiceClient client = new FlexiCaptureWebServiceClient("https://FlexiCaptureApplicationServer")) {
// Please uncomment the line below and provide a company name to interact with FlexiCapture Cloud or tenant.
// client.setTenant("Company");
// Please provide valid FlexiCapture user credentials.
client.setCredentials(new Credentials("Username", "Password"));
// Getting the FlexiCapture project. Please provide a project name here.
try (FlexiCaptureProject project = client.getProject("InvoiceDemoProject")) {
// Preparing a file to upload to FlexiCapture. The 'FileName' and 'Base64Content' are Pega function parameters.
List<File> files = new ArrayList<File>();
files.add(new File(FileName, Base64.getDecoder().decode(Base64Content.replace("\n", "").replace("\r", ""))));
// Preparing a job for a FlexiCapture batch processing.
BatchCreationJob job = new BatchCreationJob(files);
job.setName("Pega_" + System.currentTimeMillis());  // Setting FlexiCapture batch name.
job.setType("Default");                             // Setting FlexiCapture batch type name.
// FlexiCapture batch creation and run.
int batchId = project.create(job);
// Waiting for the batch to be processed and getting the processing results.
Result result = project.getResult(batchId);
// Check if the batch stage is Processed.
if (result.getStage() == com.abbyy.connectors.models.Stage.Processed) {
// Returning captured fields as a JSON string.
return result.getFields();
} else {
// Throwing an exception other stages.
// Instead of the throwing, processing of these stages can be configured here.
throw new FlexiCaptureConnectorException(String.format("Unsupported FlexiCapture stage type '%s'.", result.getStage()));
}
}
} 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 FlexiCaptureConnectorException(message);
}
```

## Next steps

Upload your project to the ABBYY FlexiCapture Application Server, and then configure it. For more information, see [Configure ABBYY FlexiCapture for the Pega connector](/flexi-capture/connectors/pega/pega-configuring-fc).
