One Park Financial
One Park Financial

Reputation: 11

Sending docusign template emails with Salesforce Opportunity Information using DocusignAPI

We are using the Docusign API to send emails to people who have filled out a form. We are using the CreateEnvelopeFromTemplatesAndForms class and have the document sending to the right recipient with the right roles and everything. The template has salesforce fields attached to different labels. When we send the template via the docusign-salesforce integration, everything populates. We are drawing a blank to figure out how to pass the opportunity ID information so the template knows what source to get info from.

Public class DS_Recipe_Send_Env_Email_Controller {
    // Send an envelope via email for signing
    // Copyright (c) 2016 DocuSign, Inc. 
    // LICENSE: The MIT License, see https://opensource.org/licenses/MIT

    // SETTINGS
    Private static string integration_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
    Private static string account_id =      'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; 
        // NOTE: You MUST use the long form of the account id. It's has 32 digits 
        // with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). 
        // This version of the account id is shown in the APIs and Connects section
        // of your DocuSign Administration tool


    Public string signer_email {get;set;}   // Required
    Public string signer_name {get;set;}    // Required
    Public string email_message {get;set;}  // Optional
    public string oppId ;
    public string accountName ;
    Public string output {get;set;}
    Public string envelope_id {get;set;}
    Public string error_code {get;set;} // Null means no error
    Public string error_message {get;set;}

    // Using Legacy authentication via an SFDC Named Credential
    //Private static string ds_server = 'callout:DocuSign_Legacy_Demo/api/3.0/dsapi.asmx';

    // If you choose to not use a named credential:
    Private static string ds_server = 'https://demo.docusign.net/api/3.0/dsapi.asmx'; //https://www.docusign.net/api/3.0/dsapi.asmx

    Private static string trace_value = 'SFDC_004_SOAP_email_send'; // Used for tracing API calls
    Private static string trace_key = 'X-ray';
    Private DocuSignTK.APIServiceSoap api_sender = new DocuSignTK.APIServiceSoap();

    Public DS_Recipe_Send_Env_Email_Controller(){}

    Public void send(){
        configure_sender();
        do_send();

        if (no_error()) {
            output = '<h2>Envelope Sent!</h2>';
            output += '<p>Envelope ID: ' + envelope_id + '</p>';
            output += '<p></p><p>Signer: ' + signer_name + ' <' + signer_email + '></p>';
        } else {
            output = '<h3>Problem</h3><p>' + error_message + '</p>';
        }
    }
    public void send(String oppId)
    {
        if(String.isBlank(oppId)) return ; 
        Opportunity opp = [SELECT Id, First_Owner__r.Name, First_Owner__r.Email,  Account.Name FROM Opportunity WHERE ID = :oppId ] ;
        this.signer_name = opp.First_Owner__r.Name ;
        this.signer_email = opp.First_Owner__r.Email ;
        this.oppId = oppId ;
        this.accountName = opp.Account.Name ;
        send();
    }
    Private void configure_sender(){
        api_sender.endpoint_x = ds_server;
        api_sender.inputHttpHeaders_x = new Map<String, String>();
        String auth =   '<DocuSignCredentials><Username>xxxxx</Username>' 
                      + '<Password>xxxxxx</Password>' 
                      + '<IntegratorKey>' + integration_key + '</IntegratorKey></DocuSignCredentials>';

        api_sender.inputHttpHeaders_x.put('X-DocuSign-Authentication', auth);
        //api_sender.timeout_x = 120000 ;
    }

    Private void do_send() {
        // Sends a signing request to signer_name / signer_email

        // Check input
        if (String.isBlank(signer_email) || String.isBlank(signer_name) || !signer_email.contains('@')) {
            error_message = 'Please fill in the email and name fields';
            error_code = 'INPUT_PROBLEM';
            return;
        }

        // Check configuration
        if (integration_key == 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ||
            account_id == 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx') { 
            error_message = 'Please configure the Apex class DS_Recipe_Send_Env_Email_Controller with your integration key and account id.';
            error_code = 'CONFIGURATION_PROBLEM';
            return;
        }

        // File contents are provided here
        // The documents array can include multiple documents, of differing types.
        // All documents are converted to pdf prior to signing.
        // The fileExtension field defaults to "pdf".       


        DocuSignTK.Recipient recipient = new DocuSignTK.Recipient();
        recipient.Email = signer_email;
        recipient.UserName = signer_name;
        recipient.ID = 1;
        recipient.Type_x = 'Signer';
        recipient.RoutingOrder = 1;
        recipient.RoleName = 'Signer 1';

        DocuSignTK.ArrayOfRecipient1 recipients = new DocuSignTK.ArrayOfRecipient1();
        recipients.Recipient = new DocuSignTK.Recipient[1];
        recipients.Recipient[0] = recipient;

        //This Creates the template to be used. Change for production
        DocuSignTK.TemplateReference templateReference = new DocuSignTK.TemplateReference();
        templateReference.Template =  '189c16de-b959-4dc6-bad6-ff47ef66468d';// b3bd42eb-313d-4c2a-bf97-04f2822d1c90
        templateReference.TemplateLocation = 'Server';

        DocuSignTK.ArrayOfTemplateReferenceRoleAssignment Roles = new DocuSignTK.ArrayOfTemplateReferenceRoleAssignment();
        Roles.RoleAssignment = new DocuSignTK.TemplateReferenceRoleAssignment[1];

        DocuSignTK.TemplateReferenceRoleAssignment role = new DocuSignTK.TemplateReferenceRoleAssignment();
        role.RoleName = 'Signer 1';
        role.RecipientID = 1;
        Roles.RoleAssignment[0] = role;
        templateReference.RoleAssignments = Roles; 

        DocuSignTK.CustomField customField1 = new DocuSignTK.CustomField();
        customField1.Name = '##SFOpportunity';
        customField1.Value = '0063B000006Pdvi';


        DocuSignTK.ArrayOfCustomField CustomFieldArray = new DocuSignTK.ArrayOfCustomField();
        CustomFieldArray.CustomField = new DocuSignTK.CustomField[1];
        CustomFieldArray.CustomField[0] = customField1;


        // Create an envelope and fill it in
        DocuSignTK.EnvelopeInformation envelopeInfo = new DocuSignTK.EnvelopeInformation();
        envelopeInfo.Subject = 'One Park Financial Application for your Signature '+ recipient.UserName;                   
        envelopeInfo.AccountId  = account_id; 
        envelopeInfo.EmailBlurb = 'I am sending you this request for your electronic signature, please review and electronically sign by following the link below.';
        envelopeInfo.CustomFields = CustomFieldArray;



        // Make the call
        try {
            //DocuSignTK.EnvelopeStatus result = api_sender.CreateAndSendEnvelope(envelope);
            DocuSignTK.ArrayOfTemplateReference TemplateReferenceArray = new DocuSignTK.ArrayOfTemplateReference();
            TemplateReferenceArray.TemplateReference = new DocuSignTK.TemplateReference[1];
            TemplateReferenceArray.TemplateReference[0] = templateReference;
            DocuSignTK.EnvelopeStatus result = new DocuSignTK.EnvelopeStatus();
            if(!Test.isRunningTest()) result = api_sender.CreateEnvelopeFromTemplatesAndForms( TemplateReferenceArray, recipients, envelopeInfo, true);
            envelope_id = result.EnvelopeID;
            System.debug('Returned successfully, envelope_id = ' + envelope_id );
        } catch ( CalloutException e) {
            System.debug('Exception - ' + e );
            error_code = 'Problem: ' + e;
            error_message = error_code;
        }      
    }

    Private Boolean no_error() {
        return (String.isEmpty(error_code));
    }


}

Upvotes: 0

Views: 667

Answers (1)

One Park Financial
One Park Financial

Reputation: 11

I found the solution. I was on the right path by passing custom salesforce variables

    DocuSignTK.CustomField customField1 = new DocuSignTK.CustomField();
    customField1.Name = '##SFOpportunity';
    customField1.Value = '0063B000006Pdvi';

My template, however also needed the account information as well. It wouldn't directly get the account ID from the opportunity.

Upvotes: 1

Related Questions