1. Creating and Sending a Package

This guide shows you how to create and send your first document package with the APEX SDK.

This guide will walk you through the steps necessary to create and send a document package (transaction in the new UI) with the OneSpan Sign APEX SDK, including, downloading the APEX SDK, and creating an APEX class.

Requirements

Below are a few requirements for following along with this guide.

Downloading the APEX SDK

Before you can use the APEX SDK, you will need to download it. You can find the download by visiting the link below.

Download the APEX SDK

Creating a Salesforce Developer Account

For this guide, you will need a Salesforce developer account. You can register for free from the link below.

Salesforce Developer

Create and Send a Package

Now that you have all of the essentials for completing the guide, you can go ahead and get started.

Installing and Configuring the SDK
The SDK can be installed using the
Deploy to Salesforce
button.

Alternatively, it can be installed as an unmanaged package:

After installing in your sandbox or developer org, you will need to configure the connection settings by creating an entry in Custom Setting. Please ensure that you have an entry with the API name “e-SignLive Connection Settings” in the org and it has the following fields in it:

  • Endpoint__c
  • API_Key__c

Capture

Capture

Once you have the Custom Setting in place, you will need to ensure that you have a record in the above defined Custom Setting with a name as “Main”. If you don’t have a record in the Custom Setting, go the Custom Setting Detail page and click on “Manage”. Then in the page click on “New” and enter the Name as “Main” without the quotes and then enter your OneSpan Sign endpoint (instance endpoint + “/api”, for example, “https://sandbox.esignlive.com/api”) and the API key in the appropriate fields and click on “Save”.

Capture

Capture

Next step, in order for the calls to go through from Salesforce, you will need to add the remote site endpoint (same endpoint as last step, but without “/api”) to the “Remote Site Settings” of the Salesforce org from which you intend to use the SDK. Once you add the Remote Site, ensure that it is marked as “Active”.

Capture

This completes the setup and configuration. You are now ready to write the code in your APEX class.

The Code

In this section, the code below will be broken down section by section. You can download the sample code from the Developer Community Code Share.

public class OssCreatePackage{
 
    public void createPackage() {
        //Create esl client
        ESignLiveSDK sdk = new ESignLiveSDK();
         
        //Create package
        ESignLiveAPIObjects.Package_x pkg = new ESignLiveAPIObjects.Package_x();
        pkg.name = 'Example Package Using APEX SDK1 - ' + Datetime.now().format();
         
        String packageId = sdk.createPackage(pkg);
        System.debug('PackageId: ' + packageId);
        
        //Add Signer
        String roleResponse = sdk.addRole('John', 'Smith', 'signer1@mailinator.com', packageId);
         
        //Add Document
        StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'testdoc1' LIMIT 1];
        Map doc = new Map();
        doc.put('Sample Document', sr.Body);
         
        ESignLiveAPIObjects.Document document = new ESignLiveAPIObjects.Document();
        document.name = 'Sample Contract';
        document.id = 'document1';
         
        sdk.createDocuments(packageId, document, doc);

        //Add Signature
        Map role = (Map)JSON.deserializeUntyped(roleResponse);
        String roleId = (String)(role.get('id'));
         
        ESignLiveAPIObjects.Field field = new ESignLiveAPIObjects.Field();
        field.left = 208;
        field.width = 200;
        field.height = 50;
        field.top = 518;
        field.page = 0;
        field.subtype = 'CAPTURE';
        field.type = 'SIGNATURE';
         
        List fields = new List();
        fields.add(field);
         
        ESignLiveAPIObjects.Approval approval = new ESignLiveAPIObjects.Approval();
        approval.fields = fields;
        approval.role = roleId;
         
        String signatureResponse = sdk.addSignature(packageId, 'document1', approval);
         
        //Send package
        pkg.status = ESignLiveAPIObjects.PackageStatus.SENT;
         
        sdk.updatePackage(pkg, packageId);     
    }
}

The first line creates your OneSpan Sign client, it will internally use your OneSpan Sign account’s API Key and the appropriate API URL that you defined in your Custom Settings.

ESignLiveSDK sdk = new ESignLiveSDK();

The next block of code is where you create the document package. Within this block, you give your package a name.

ESignLiveAPIObjects.Package_x pkg = new ESignLiveAPIObjects.Package_x();
pkg.name = 'Example Package Using APEX SDK1 - ' + Datetime.now().format();
         
String packageId = sdk.createPackage(pkg);
System.debug('PackageId: ' + packageId);

Next, one signer is added to the package. This is the person you will send the package to for signature. In this block, you will replace the email address and name with your test info.

String roleResponse = sdk.addRole('John', 'Smith', 'mail32@mailinator.com', packageId);

Now, you add your document to be signed and also place the signature block for your signer. Again, remember to replace the sample values in this block with your test info.

//Add Document
StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'testdoc1' LIMIT 1];
Map doc = new Map();
doc.put('Sample Document', sr.Body);
         
ESignLiveAPIObjects.Document document = new ESignLiveAPIObjects.Document();
document.name = 'Sample Contract';
document.id = 'document1';
         
sdk.createDocuments(packageId, document, doc);
         
//Add Signature
Map role = (Map)JSON.deserializeUntyped(roleResponse);
String roleId = (String)(role.get('id'));
         
ESignLiveAPIObjects.Field field = new ESignLiveAPIObjects.Field();
field.left = 208;
field.width = 200;
field.height = 50;
field.top = 518;
field.page = 0;
field.subtype = 'CAPTURE';
field.type = 'SIGNATURE';
         
List fields = new List();
fields.add(field);
         
ESignLiveAPIObjects.Approval approval = new ESignLiveAPIObjects.Approval();
approval.fields = fields;
approval.role = roleId;
         
String signatureResponse = sdk.addSignature(packageId, 'document1', approval);

In this example, we add pdf in Static Resources in your Salesforce Developer account and read it as a blob type.

Capture

Now that your package is ready, you use your OneSpan Sign client to send it to the signer.

//Send package
pkg.status = ESignLiveAPIObjects.PackageStatus.SENT;
         
sdk.updatePackage(pkg, packageId);  

Running the Code

With your APEX class completed, you can go ahead and execute it. Open up your Developer Console and press Ctrl + E to launch the Execute Anonymous Window. Type in:

new OssCreatePackage.createPackage();

And hit Execute.
You can see your package ID printed in debug console:

Capture

If you check your OneSpan Sign Sandbox account inbox, you will see that the package has been created and waiting for signature. Here is an example of what you might expect:

Capture

Get the Code