使用嵌入式签名将自定义合并字段填充到Docusign模板中

时间:2018-01-30 14:17:30

标签: docusignapi

在Salesforce社区中,我们使用Docusign Connect进行嵌入式签名,以便客户对文档进行签名。我们在填充模板中的合并字段时遇到了问题。我们正在尝试使用DocuSignAPI.CustomField和DocuSignAPI.ArrayOfCustomField,但无法这样做。有人可以查看代码并帮助我们确定问题吗?谢谢!

    /*=====================================================================
SendToDocuSignController 
=====================================================================*/

public without sharing class SendToDocuSignController {
    private final list<Contact> lstcontact ;
    private final Application__c objApplication;
    private final list<DocuSign_Lease_Mapping__c> lstDocuSignLeaseMapping;
    private final list<AddOns__c> lstAddOns;
    Private static string trace_value = 'SFDC_002_SOAP_sender_view'; // Used for tracing API calls
    Private static string trace_key = 'X-ray'; 

   // Demo code
    Public string sender_view_url {get;set;} 
    Public string sender_return_url {get;set;} // Required. Where DS redirects to after sending 
    Public string signer_email {get;set;}   // Required
    Public string signer_name {get;set;}    // Required
    Public string email_message {get;set;}  // Optional
    Public string output {get;set;}
    Public string error_code {get;set;} // Null means no error
    Public string error_message {get;set;}
 // Demo code
    public String envelopeId {get;set;}
    private Constants__c objConstantSetting = Constants__c.getOrgDefaults();
    private String accountId = 'd66c97b3-f210-41c5-8dcc-1c617d4431d8';  //'3753234'
    private String userId = objConstantSetting.Docusign_User_ID__c;
    private String password = objConstantSetting.Docusign_Password__c;
    private String integratorsKey = objConstantSetting.Docusign_Integrator_Key__c;
    private String webServiceUrl = objConstantSetting.Docusign_webServiceUrl__c;

    public SendToDocuSignController()
    {
        System.debug('lstcontact');
        String ApplicationID = 'a070t000000EF9Y';  //a070t000000MBpi 
        this.lstAddOns = [SELECT Id ,Name ,Application__c FROM AddOns__c where Application__c =:ApplicationID ];
        this.objApplication = [select Id, Household__c, Property__c  from Application__c where id = :ApplicationID ];
        this.lstcontact = [select Id, Name, RecordType.Name , Email from Contact where RecordType.Name != 'Minor Child' AND Email !=  null AND AccountId =:objApplication.Household__c ];
        System.debug('lstcontact'+lstcontact);
        if(lstAddOns.isEmpty())
        this.lstDocuSignLeaseMapping  = [select DocuSign_Template_ID__c, Name, Property__c from DocuSign_Lease_Mapping__c where Property__c =: objApplication.Property__c ];
        else
        this.lstDocuSignLeaseMapping  = [select DocuSign_Template_ID__c, Name, Property__c from DocuSign_Lease_Mapping__c where Property__c =: objApplication.Property__c AND   Concession_Addendum__c = false];
        envelopeId = 'Not sent yet';  
    }

    /*****************************************************************
    Method Name : SendNow()
    Purpose: Creates Docusign envelope and sends an email from docusign to sign the docs

    ******************************************************************/
    public PageReference SendNow()
    {
        DocuSignAPI.APIServiceSoap dsApiSend = new DocuSignAPI.APIServiceSoap();
        dsApiSend.endpoint_x = webServiceUrl;
        //Set Authentication
        String auth = '<DocuSignCredentials><Username>'+ userId 
            +'</Username><Password>' + password 
            + '</Password><IntegratorKey>' + integratorsKey 
            + '</IntegratorKey></DocuSignCredentials>';
        System.debug('Setting authentication to: ' + auth);

        dsApiSend.inputHttpHeaders_x = new Map<String, String>();
        dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication', auth);
        dsApiSend.inputHttpHeaders_x.put(trace_key, trace_value);

        //Create Docusign envelope information object 
        DocuSignAPI.EnvelopeInformation EnvelopeInformation = new DocuSignAPI.EnvelopeInformation ();
        EnvelopeInformation.Subject = Label.Docusign_Email_Subject ;
        EnvelopeInformation.EmailBlurb = ' ';
        EnvelopeInformation.AccountId  = objConstantSetting.Docusign_Account_ID__c; 
        System.debug('EnvelopeInformation'+EnvelopeInformation);

        DocuSignAPI.CustomField field = new DocuSignAPI.CustomField (); 
        field.Name = 'DSFSSourceObjectId'; //'dsfs__Application__c'; 
        field.Value = 'a070t000000EF9Y'; //'01I0t0000000FK3'; 
        field.Show = 'true';
        field.CustomFieldType = 'Text';
        DocuSignAPI.ArrayOfCustomField arrayOfCustomField = new DocuSignAPI.ArrayOfCustomField();
        arrayOfCustomField.CustomField  = new DocuSignAPI.CustomField[1];
        arrayOfCustomField.CustomField[0] = field;
        EnvelopeInformation.CustomFields = arrayOfCustomField;
        System.debug('PB: field.Name & field.value = '+ field.Name +field.value);

        //Create Docusign TemplateReference object 
        DocusignAPI.TemplateReference reference = new DocusignAPI.TemplateReference();
        DocuSignAPI.ArrayOfTemplateReference templateReferenceArray = new DocuSignAPI.ArrayOfTemplateReference();
        templateReferenceArray.TemplateReference = new DocuSignAPI.TemplateReference[lstDocuSignLeaseMapping.size()];
        System.debug('lst of template'+lstDocuSignLeaseMapping);
        System.debug('lst of lstDocuSignLeaseMapping.size()'+lstDocuSignLeaseMapping.size());


        Integer seq =0;
        Integer index = 0;
        Integer signerNumber = 1;
        //Create Docusign recipient object
        DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
        DocuSignAPI.ArrayOfRecipient1 Recipients = new DocuSignAPI.ArrayOfRecipient1();
        Recipients.Recipient = new DocuSignAPI.Recipient[lstcontact.size()];


        DocuSignAPI.TemplateReferenceRoleAssignment roleAssignment1; 
        DocuSignAPI.TemplateReferenceRoleAssignment roleAssignment2; 

        for(Contact objcontact : lstcontact) {

            recipient = new DocuSignAPI.Recipient();
            recipient.ID = signerNumber;
            recipient.Type_x = 'Signer';
            recipient.roleName = 'Signer ' + String.valueOf(signerNumber);
            recipient.RoutingOrder = signerNumber;
            recipient.Email = objcontact.Email; 
            recipient.UserName = objcontact.Name ;           
            recipient.RequireIDLookup = false;
            recipient.CaptiveInfo = new DocuSignAPI.RecipientCaptiveInfo(); 
            recipient.CaptiveInfo.ClientUserId = objcontact.Id;
            System.debug('recipient'+recipient); 
            Recipients.Recipient[seq] = recipient;

            if(signerNumber == 1) {
                roleAssignment1 = new  DocuSignAPI.TemplateReferenceRoleAssignment();
            roleAssignment1.RoleName = recipient.RoleName;
                roleAssignment1.RecipientID = recipient.ID;
            }
            if(signerNumber == 2) {
                roleAssignment2 = new  DocuSignAPI.TemplateReferenceRoleAssignment();
            roleAssignment2.RoleName = recipient.RoleName;
             roleAssignment2.RecipientID = recipient.ID;
            }
            signerNumber++;
            seq++;
        } 
        seq = 1;


        for(DocuSign_Lease_Mapping__c objDocuSignLeaseMapping : lstDocuSignLeaseMapping){
            reference = new DocusignAPI.TemplateReference();

            reference.TemplateLocation = 'Server';
            reference.Template =  objDocuSignLeaseMapping.DocuSign_Template_ID__c;

            reference.sequence = seq;  
            reference.RoleAssignments = new DocuSignAPI.ArrayOfTemplateReferenceRoleAssignment();
            reference.RoleAssignments.RoleAssignment = new DocuSignAPI.TemplateReferenceRoleAssignment[3];
            reference.RoleAssignments.RoleAssignment[0] = roleAssignment1;
            reference.RoleAssignments.RoleAssignment[1] = roleAssignment2;
            templateReferenceArray.TemplateReference[index] = reference;
            index++;
            seq++;
        }


        //create and send envelope with template
        try {
            //DocuSignAPI.EnvelopeStatus es1 = dsApiSend.CreateAndSendEnvelope(EnvelopeInformation);
            DocuSignAPI.EnvelopeStatus es 
            = dsApiSend.CreateEnvelopeFromTemplates(templateReferenceArray, Recipients ,EnvelopeInformation,true);
            envelopeId = es.EnvelopeID;

            DocuSignAPI.RequestRecipientTokenClientURLs clientURLs = new DocuSignAPI.RequestRecipientTokenClientURLs();
            Blob b = Crypto.GenerateAESKey(128);
            String h = EncodingUtil.ConvertTohex(b);
            String guid = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
            DocuSignAPI.RequestRecipientTokenAuthenticationAssertion assertion = new DocuSignAPI.RequestRecipientTokenAuthenticationAssertion();
            assertion.AssertionID = guid;
            assertion.AuthenticationInstant = DateTime.Now();
            assertion.AuthenticationMethod = 'Email';
            assertion.SecurityDomain = 'force.com';
            // Construct the URLs based on username
            DocuSignAPI.RequestRecipientTokenClientURLs urls = new DocuSignAPI.RequestRecipientTokenClientURLs();
            String urlBase = URL.getSalesforceBaseUrl().toExternalForm()+'/'+lstcontact[0].id ;
            clientURLs.OnSigningComplete = urlBase + '?event=SignComplete&uname=' + recipient.UserName;
            clientURLs.OnViewingComplete = urlBase + '?event=ViewComplete&uname=' + recipient.UserName;
            clientURLs.OnCancel = urlBase + '?event=Cancel&uname=' + recipient.UserName;
            clientURLs.OnDecline = urlBase + '?event=Decline&uname=' + recipient.UserName;
            clientURLs.OnSessionTimeout = urlBase + '?event=Timeout&uname=' + recipient.UserName;
            clientURLs.OnTTLExpired = urlBase + '?event=TTLExpired&uname=' + recipient.UserName;
            clientURLs.OnIdCheckFailed = urlBase + '?event=IDCheck&uname=' + recipient.UserName;
            clientURLs.OnAccessCodeFailed = urlBase + '?event=AccessCode&uname=' + recipient.UserName;
            clientURLs.OnException = urlBase + '?event=Exception&uname=' + recipient.UserName;
            // assumes apiService = preconfigured api proxy
            String token;
        try {

            token = dsApiSend.RequestRecipientToken(envelopeId,Recipients.Recipient[0].captiveinfo.ClientUserId,Recipients.Recipient[0].UserName,Recipients.Recipient[0].Email,assertion,clientURLs);
            String token1 = dsApiSend.RequestRecipientToken(envelopeId,Recipients.Recipient[1].captiveinfo.ClientUserId,Recipients.Recipient[1].UserName,Recipients.Recipient[1].Email,assertion,clientURLs);
            system.debug('????????????token1??????????' + token1);

        }
          catch ( CalloutException e) {
                System.debug('Exception - ' + e );
                envelopeId = 'Exception - ' + e;
        }
            PageReference retURL = new PageReference(token);
            retURL.setRedirect(false);
            return retURL;
    }
        catch ( CalloutException e) {
            System.debug('Exception - ' + e );
            envelopeId = 'Exception - ' + e;
            return null;
        }

    }
        private String getPopURL() {
        String popURL = String.valueOf(System.URL.getSalesforceBaseUrl());
        if (popURL == null) {
            popURL = 'https://na3.salesforce.com/apex/';
        } 
        System.Debug('pop page: ' + popURL + 'embedPop');
        return popURL + 'embedPop';
    }
}

0 个答案:

没有答案