Salesforce DocuSign Integration

In this blog, I am going to explain how to use DocuSign’s API in Salesforce. DocuSign is having support for both the SOAP and REST API support. This article is the target for DocuSign SOAP API only.
What you are going to learn in this article are.
 How to use DocuSign API
How to create a template for DocuSign signature requests / e-sign
How to integrate it with APEX

Pre Requisitions:

         DocuSign Account – Register from  (https://www.docusign.com)

         DocuSign For Salesforce Package installed and configured.

Let’s get started.

Step 1:  Download DocuSign WSDL.

        Go to the below URL and download the DocuSign WSDL file.

       https://www.docusign.net/api/3.0/schema/dsapi.wsdl

 Step 2: Generated Apex Class from WSDL.

        In order to create the Apex class from DocuSign WSDL, I am utilizing Salesforce WSDL to apex class features.

        Go to Setup – > Develop – > Apex Classes – >generate from WSDL -> Chose the downloaded DocuSign WSDL

Step 3: Remote Site Settings

        Add below URL to remote Site settings. Remote site settings URL will differ from the DocuSign Sandbox vs. DocuSign Production 

          https://www.docusign.net

Step 4: Generate template for your e-signature.

        You can able to use any of static documents or visual force pages for DocuSign e-sign documents. 

        Now you are going to create a visual force page pdf that needs to send for DocuSign signature.

The below code is the key are in a visual force page which used to capture the recipient signature and print name and date.

 

           

By Completeing DocuSign, You are agreed  terms and Conditions .

           

Customer Name: (Please print) cstnamtag

       

Signed: Signaturetag

       

Date: signdatetag

 

On the above code, three id tags are there namely cstnamtag , Signaturetag and signdatetag .We will use these tags to place signature and Date and print name on DocuSign e-copy document.  

Step 5: triggering the e- signature.

   Here is the sample button that used to send the DocuSign to the end user.

On Click of Send DocuSign, button end user will receive an email with attachment need to be signed. 

Step 6: Docu Sign Terminology and Code Walkthrough

  Some common terms you need to understand. 

Document

    A digital file that contains content to be reviewed and/or signed or initialed by one or more recipients. DocuSign accepts almost all document types – for example .pdf, .docx, .rtf, .png – and you can store multiple documents in a single envelope.

Envelope

      An envelope is a container or “package” that is used to send documents to recipients and manage transactions. Envelopes have statuses (i.e. sent, delivered, completed, voided) and typically contain documents, recipients, and tabs.  

Recipient

       Someone who receives an envelope and, depending on the settings, can sign the documents or add information where indicated by tabs. Recipients do not need a DocuSign account to sign or participate in transactions, and there are seven (7) different recipient types available in the platform. When you embed document signing into your UI your recipients are known as embedded recipients; users who sign through the DocuSign website are known as remote recipients.

Tab

        A DocuSign Tab – also called a Field or Tag – are used in several ways. First, they are used to indicate to a recipient where a signature or initials are required.

Second, tabs can be used to show data or information to recipients, such as dates, company names, titles, etc.

Third, tabs may be used as editable information fields where signers can add data to a document.

Code Walkthrough. 

 Below piece shows BLOB that will send as Document through DocuSign 

[code language=”java”]
Blob SignDocument= Blob.valueOf(‘ ‘);
PageReference pr = new PageReference(‘/apex/DocuSignDynamicGeneration’) ;
SignDocument = pr.getContentAsPDF();
[/code]

Below piece of code shows the Authentication for DocuSign API. 

[code language=”java”]
DocuSignAPI.APIServiceSoap dsApiSend = new DocuSignAPI.APIServiceSoap();
dsApiSend.endpoint_x = webServiceUrl;
dsApiSend.timeout_x=120000;

String auth = ‘<DocuSignCredentials><Username>’+ userId
+’</Username><Password>’ + password
+ ‘</Password><IntegratorKey>’ + integratorsKey
+ ‘</IntegratorKey></DocuSignCredentials>’;

dsApiSend.inputHttpHeaders_x = new Map<String, String>();
dsApiSend.inputHttpHeaders_x.put(‘X-DocuSign-Authentication’,auth);
[/code]

Below piece of code shows  how to create an Envelope and Document for doucSIgn 

[code language=”java”]

DocuSignAPI.Envelope envelope = new DocuSignAPI.Envelope();
envelope.Subject = ‘DocuSign from Raj’;
envelope.EmailBlurb = ‘Review review the documnet and sign ‘;
envelope.AccountId = accountId;

DocuSignAPI.Document document = new DocuSignAPI.Document();
document.ID = 1;
document.pdfBytes = EncodingUtil.base64Encode(SignDocument);
document.Name = ‘Contract infomration’;
document.FileExtension = ‘.pdf’;
envelope.Documents = new DocuSignAPI.ArrayOfDocument();
envelope.Documents.Document = new DocuSignAPI.Document[1];
envelope.Documents.Document[0] = document;
[/code]

Below piece of code shows the how to set recipient details 

[code language=”java”]

DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
recipient.ID = 1;
recipient.Type_x = ‘Signer’;
recipient.RoutingOrder = 1;
recipient.Email = ‘aaaaaaaa@gmail.com’;
recipient.UserName = ‘aaaaaaaaaaaaaaaa’;
recipient.SignerName = ‘Raj ‘;

DocuSignAPI.Recipient recipient1 = new DocuSignAPI.Recipient();
recipient1.ID = 2;
recipient1.Type_x = ‘CarbonCopy’;
recipient1.RoutingOrder = 1;
recipient1.UserName = ‘mohan’;
recipient1.Email = ‘aaaaaaaaaaaaaaa.v@gmail.com’;

DocuSignAPI.Recipient recipient2 = new DocuSignAPI.Recipient();
recipient2.ID = 3;
recipient2.Type_x = ‘CarbonCopy’;
recipient2.RoutingOrder = 1;
recipient2.UserName = ‘Raj ‘;
recipient2.Email = ‘aaaaaaaaaaaaaaaa@outloo.com’;
[/code]

Below code shows how to place the signed and Print name and date of DocuSign Documents that need to be signed by the user .

[code language=”java”]

DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
tab1.Type_x = ‘SignHere’;
tab1.RecipientID = 1;
tab1.DocumentID = 1;
tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
tab1.AnchorTabItem.AnchorTabString = ‘Signaturetag’;

DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
tab2.Type_x = ‘DateSigned’;
tab2.RecipientID = 1;
tab2.DocumentID = 1;
tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
tab2.AnchorTabItem.AnchorTabString = ‘signdatetag’;

DocuSignAPI.Tab tab4 = new DocuSignAPI.Tab();
tab4.Type_x = ‘FullName’;
tab4.RecipientID = 1;
tab4.DocumentID = 1;
tab4.AnchorTabItem = new DocuSignAPI.AnchorTab();
tab4.AnchorTabItem.AnchorTabString = ‘cstnamtag’;
[/code]

finally creating envelop that will trigger an email to user and store in DocuSign Magamed Package object “dsfs__DocuSign_Status__c” to track the Status like Send, Completed and Voided or decline etc. 

[code language=”java”]

DocuSignAPI.EnvelopeStatus EnvStatus = dsApiSend.CreateAndSendEnvelope(envelope);
String envelopeId = EnvStatus.EnvelopeID;

dsfs__DocuSign_Status__c DocStatus = new dsfs__DocuSign_Status__c();
DocStatus.dsfs__DocuSign_Envelope_ID__c = envelopeId;
DocStatus.dsfs__Sender__c = userinfo.getusername();
DocStatus.dsfs__Sender_Email__c = userinfo.getuseremail();
DocStatus.dsfs__Subject__c = envelope.Subject;
DocStatus.dsfs__Envelope_Status__c = EnvStatus.status;
insert DocStatus;
[/code]

Step 7: tracking status for DocuSign.

Once DocuSign is sent to the User, you can track the DocuSign status on dsfs__DocuSign_Status__c t.

Complete code URL.

https://github.com/rajamohanvakati/DocuSign