divyayerubandi… | Posts: 4

Issue with Package creation usinf Salesforce Apex Rest API

0 votes

I am creating a package with a pdf file and JSON Payload with Package status as 'SENT'. It is creating the Packae successfully in Postman. But when I used the same file and JSON payload in the Apex class and posted the API it gave the response as a BAD request. May I know what may be the issue? 
If I Created the package with STATUS as DRAFT, It is creating the package but I am not able to generate the signing URL. 

The below URL is the API I have used.
POST : https://sandbox.esignlive.com/api/packages

ANy help would be appreciated.


Approved Answer
Duo_Liang | Posts: 3776

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Bhargava,

 

I see, this is due to the line:

   requestBody += staticRes.Body+ '\r\n';
Where the blob was concatenated with string directly. 

Due to the complex nature of handling blob and string in Apex, I would suggest to take advantage of a latest OSS API change to create package where OSS allows to pass in base64 encoded document string, see below:

 

StaticResource res = [
   SELECT Body
   FROM StaticResource
   WHERE Name = 'texttagdoc'
   LIMIT 1
];

 

String jsonBody = '{"documents": [{"id": "MyDocumentID","base64Content":"'+EncodingUtil.base64Encode(res.Body)+'", "fields": [{"value": "22/01/2024","name": "RequestDate"}, {"value": "Boston Scientific","name": "CompanyName"}, {"value": "200 E MAIN ST","name": "Address"}, {"value": "400 E MAIN ST","name": "BillTo"}, {"value": "400 E MAIN ST","name": "ShipTo"}, {"value": "BSC","name": "NewCustomerName"}, {"value": "John Smith","name": "ContactName"}, {"value": "Boston Scientific","name": "BillingName"}, {"value": "[email protected]","name": "NewCustomerEmailAddress"}, {"value": "[email protected]","name": "NewBillingEmailAddress"}, {"value": "200 E MAIN ST,\\nPHOENIX AZ, 85123 USA","name": "CurrentShipTo"}, {"value": "There are changes in my Org \\nPHOENIX AZ, 85123 USA","name": "Explanation"}], "extract": true, "extractionTypes": ["TEXT_TAGS"], "name": "Sample Contract"}], "type": "PACKAGE", "status": "SENT", "roles": [{"id": "Signer1", "index": 0, "type": "SIGNER", "signers": [{"email": "[email protected]", "firstName": "John", "lastName": "Smith", "signerType": "EXTERNAL_SIGNER"}], "name": "Signer1"}], "name": "Field Injection Example V2-1", "settings": {"ceremony": {"handOver": {"text": "Back to Join", "title": "Click here to go back to Join", "href": "https://www.google.com", "autoRedirect": true, "parameters": ["PACKAGE", "SIGNER", "STATUS"]}}}}';

 


  // Send the request to create the package
  HttpRequest request = new HttpRequest();
  request.setEndpoint('https://sandbox.esignlive.com/api/packages');
  request.setMethod('POST');
  request.setHeader('Authorization', 'Bearer ' + accessToken);
  request.setHeader('Content-Type', 'application/json');
  request.setBody(jsonBody);
  
  HttpResponse response = new Http().send(request);

 

Also check the API Swagger here

On the other hand, OSS Apex sdk is free to use, but sdk doesn't support this latest modelling change, this might be a reason why you want to still use your custom restful code.

 

Duo

 

 


Duo_Liang | Posts: 3776

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Bhargava,

 

Thanks for your post! 

Are you using OneSpan Sign Apex SDK, or are you sending Restful API using your own HttpRequest? Typically response 400 means the wrong request raw body format or invalid request JSON. If you are using your own code base, it's worth referring to the Apex example in our SDK code: 

OneSpanRESTAPIHelper.cls > doPost methods with either "application/json" content or "multipart/form-data" content.

You can also share some code snippets around the codes how you build the package JSON and invoke the POST call.

 

Duo
 


divyayerubandi… | Posts: 4

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Duo,

I am using Restful API with my own HTTP request. If I give the Status as "Draft" it is working as expected but not showing the record in UI of Onespan Sandbox.
Below is my code for reference 
String jsonBody = '{"documents": [{"id": "MyDocumentID", "fields": [{"value": "22/01/2024","name": "RequestDate"}, {"value": "Boston Scientific","name": "CompanyName"}, {"value": "200 E MAIN ST","name": "Address"}, {"value": "400 E MAIN ST","name": "BillTo"}, {"value": "400 E MAIN ST","name": "ShipTo"}, {"value": "BSC","name": "NewCustomerName"}, {"value": "John Smith","name": "ContactName"}, {"value": "Boston Scientific","name": "BillingName"}, {"value": "[email protected]","name": "NewCustomerEmailAddress"}, {"value": "[email protected]","name": "NewBillingEmailAddress"}, {"value": "200 E MAIN ST,\\nPHOENIX AZ, 85123 USA","name": "CurrentShipTo"}, {"value": "There are changes in my Org \\nPHOENIX AZ, 85123 USA","name": "Explanation"}], "extract": true, "extractionTypes": ["TEXT_TAGS"], "name": "Sample Contract"}], "type": "PACKAGE", "status": "DRAFT", "roles": [{"id": "Signer1", "index": 0, "type": "SIGNER", "signers": [{"email": "[email protected]", "firstName": "John", "lastName": "Smith", "signerType": "EXTERNAL_SIGNER"}], "name": "Signer1"}], "name": "Field Injection Example V2-1", "settings": {"ceremony": {"handOver": {"text": "Back to Join", "title": "Click here to go back to Join", "href": "https://www.google.com", "autoRedirect": true, "parameters": ["PACKAGE", "SIGNER", "STATUS"]}}}}';
   
   // Construct the boundary for multipart form data
   String boundary = 'xx---------webkit';
   String delimiter = '\r\n--' + boundary + '\r\n';
   String closeDelimiter = '\r\n--' + boundary + '--';
   
   // Construct the request body for multipart form data
   String requestBody = delimiter;
   requestBody += 'Content-Disposition: form-data; name="file"; filename="staticRes.pdf"\r\n';
   requestBody += 'Content-Type: application/pdf\r\n\r\n';
   requestBody += staticRes.Body+ '\r\n';
   requestBody += delimiter;
   requestBody += 'Content-Disposition: form-data; name="payload"\r\n\r\n';
   requestBody += jsonBody + '\r\n';
   requestBody += closeDelimiter;

   // Send the request to create the package
   HttpRequest request = new HttpRequest();
   request.setEndpoint('https://sandbox.esignlive.com/api/packages');
   request.setMethod('POST');
   request.setHeader('Authorization', 'Bearer ' + accessToken);
   request.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
   request.setBody(requestBody);
   
   HttpResponse response = new Http().send(request);

And I also have one doubt is Apex SDK a Paid service or a Free resource?


divyayerubandi… | Posts: 4

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Duo,

May I know how much time the Access token is valid it is saying "expiresAt": 1712749843263 How can we calculate this time? and is there any way to get rid of this after the signing URL is generated? If the user fails to sign the agreement then there is no way for him to sign and it is asking for the access. 

FYI we are not sending the mail for the agreement sign but we are opening the URL in the new tab with the click of a button.

Divya.


Duo_Liang | Posts: 3776

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Divya,

 

The oauth2 token will be valid for 30 minutes. But if you generated the signing URL using below API, the generated signing URL doesn't expire. 

GET /api/packages/{packageId}/roles/{roleId}/signingUrl

 

Duo


divyayerubandi… | Posts: 4

Reply to: Issue with Package creation usinf Salesforce Apex Rest API

0 votes

Hi Duo,

Thanks for clarifying But I can see the below error when I kept the Agreement open for some time. Is this the expected behavior?

 


Attachments

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off