求助:使用DocuSign eSign Ruby SDK预填充模板字段失败
解决DocuSign模板预填充字段失败的问题
问题根源
你当前的代码把预填充字段绑定到了具体签署者的tabs下,但预填充字段是属于整个信封的全局字段,不属于任何签署角色,这种绑定方式无法正确触发预填充逻辑。另外需要确认模板中的字段是否配置为预填充类型(即字段的收件人设置为“预填充”而非具体签署角色)。
修正后的代码
def send_data_using_template(access_token) api_client = DocuSign_eSign::ApiClient.new api_client.default_headers['Authorization'] = 'Bearer ' + access_token templates_api = DocuSign_eSign::TemplatesApi.new(api_client) envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client) options = DocuSign_eSign::ListTemplatesOptions.new options.search_text = "Example document generation template" results = templates_api.list_templates(ENV['DOCUSIGN_ACCOUNT_ID'], options) template = results.envelope_templates.find { |template| template.name == "Example document generation template" } template_id = template.template_id if template current_user_name = "Alia" current_user_email = "hiraiqbal.me@gmail.com" # 创建预填充标签集合 text_tab_name = DocuSign_eSign::Text.new(tab_label: 'name', value: 'Jabberwocky!') text_tab_title = DocuSign_eSign::Text.new(tab_label: 'title', value: 'Jabberwockyjfgnkwkgkwh!') text_tab_name1 = DocuSign_eSign::Text.new(tab_label: 'name1', value: 'Jabberwocky!') text_tab_title1 = DocuSign_eSign::Text.new(tab_label: 'title1', value: 'Jabberwockyjfgnkwkgkwh!') prefill_tabs = DocuSign_eSign::Tabs.new( text_tabs: [text_tab_name, text_tab_title, text_tab_name1, text_tab_title1] ) envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({ status: 'sent', templateId: template_id, emailSubject:" EMAIL_SUBJECT", prefill_tabs: prefill_tabs # 将预填充标签直接绑定到信封定义 }) # 签署者部分保持不变,无需在signer下添加预填充字段 signer = DocuSign_eSign::TemplateRole.new({ email: current_user_email, name: current_user_name, roleName: 'signer' }) envelope_definition.template_roles = [signer] envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client response = envelopes_api.create_envelope ENV['DOCUSIGN_ACCOUNT_ID'], envelope_definition if response.envelope_id.present? puts "Envelope created successfully" loop do envelope_status = envelopes_api.get_envelope ENV['DOCUSIGN_ACCOUNT_ID'], response.envelope_id puts "Envelope status: #{envelope_status.status}.................................." break if ['completed', 'signed'].include?(envelope_status.status) sleep 5 end response = HTTParty.get( "https://demo.docusign.net/restapi/v2.1/accounts/#{ENV['DOCUSIGN_ACCOUNT_ID']}/envelopes/#{response.envelope_id}/documents/combined", headers: { "Authorization" => "Bearer #{access_token}" } ) if response.success? timestamp = Time.now.strftime("%Y%m%d%H%M%S") signed_document_path = "config/signed_document_#{timestamp}.pdf" File.open(signed_document_path, "wb") { |file| file.write(response.body) } puts "Signed document saved successfully at #{signed_document_path}" else puts "Error retrieving signed document: #{response}" end else puts "Error creating envelope: #{response}" end end
额外检查项
- 确认模板中的字段:在DocuSign控制台编辑模板时,将需要预填充的字段的收件人设置为“预填充”(而非你代码中的
signer角色),并确保字段的Tab Label与代码中设置的tab_label完全一致(大小写敏感)。 - 如果字段是属于特定签署者的可编辑字段,而非全局预填充,则需要确保模板中该字段的收件人角色与代码中
roleName完全匹配,此时字段应放在对应TemplateRole的tabs下。
内容的提问来源于stack exchange,提问作者Hira
相关产品推荐
相关产品推荐

