使用DocusignAPI使用Salesforce机会信息发送docusign模板电子邮件

时间:2018-01-31 15:32:25

标签: salesforce docusignapi

我们正在使用Docusign API向填写表单的人发送电子邮件。我们正在使用CreateEnvelopeFromTemplatesAndForms类,并将文档发送给具有正确角色和所有内容的正确收件人。模板的salesforce字段附加到不同的标签。当我们通过docusign-salesforce集成发送模板时,所有内容都会填充。 我们正在绘制一个空白,以弄清楚如何传递机会ID信息,以便模板知道从哪个来源获取信息。

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));
    }


}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我通过传递自定义salesforce变量

走在正确的道路上
    DocuSignTK.CustomField customField1 = new DocuSignTK.CustomField();
    customField1.Name = '##SFOpportunity';
    customField1.Value = '0063B000006Pdvi';

我的模板也需要帐户信息。它不会直接从机会中获取帐户ID。