DocuSign API Java实现:如何为签署人自动填充公司与头衔字段
Absolutely! You can absolutely programmatically fill the Company and Title fields for each signer when sending documents via DocuSign's API—especially when working with templates. Let's walk through how to adjust your existing code to make this work:
Key Background First
Before diving into code, make sure your DocuSign template has the Company and Title fields added and linked to the corresponding signer roles (like "signer1", "signer2"). DocuSign's API lets you pass these values directly through the Signer and TemplateRole objects, which will auto-populate the fields for each signer.
Modified Code with Company/Title Population
Here's your updated code with the necessary additions (I've marked new lines with comments for clarity):
public ESenderSummary sendDocumentsToSignByOrderOfSigner(List<ESigner> eSigners, String templateId, List<EDocument> eDocuments) throws ESignException { String docuSignAccountId = docuSignAuthenticationProvider.getAccountId(); TemplatesApi templatesApi = docuSignAuthenticationProvider.getTemplatesApi(); List<TemplateRole> templateRoles = new ArrayList<>(); String emailSubject; EmailSettings emailSettings; Recipients recipients; try { EnvelopeTemplate envelopeTemplate = templatesApi.get(docuSignAccountId, templateId); emailSubject = envelopeTemplate.getEmailSubject(); recipients = envelopeTemplate.getRecipients(); List<Signer> signers = recipients.getSigners(); int index = 1; for (Signer signer : signers) { ESigner eSigner = eSigners.get(index - 1); // create a signer recipient to sign the document, identified by name and email signer.setEmail(eSigner.getEmail()); signer.setName(eSigner.getFirstName() + " " + eSigner.getLastName()); // NEW: Populate Company and Title for the core Signer profile signer.setCompany(eSigner.getCompany()); signer.setTitle(eSigner.getTitle()); // create a templateRole for each signer from recipient to sign the document TemplateRole templateRole = new TemplateRole(); templateRole.setEmail(eSigner.getEmail()); templateRole.setName(eSigner.getFirstName() + " " + eSigner.getLastName()); templateRole.setRoleName("signer" + index); templateRole.setRoutingOrder(String.valueOf(index)); // ordering of sign // NEW: Map Company and Title to the template's role-specific fields templateRole.setCompany(eSigner.getCompany()); templateRole.setTitle(eSigner.getTitle()); templateRoles.add(templateRole); index++; } recipients.setSigners(signers); emailSettings = envelopeTemplate.getEmailSettings(); } catch (ApiException e) { throw new ESignException("DocuSign API failed to retrieve envelope template " + templateId + ": " + e.getMessage()); } // Next, create the top level envelope definition and populate it. EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition(); envelopeDefinition.setEmailSubject(emailSubject); envelopeDefinition.setRecipients(recipients); envelopeDefinition.setEmailSettings(emailSettings); envelopeDefinition.setTemplateRoles(templateRoles); envelopeDefinition.setDocuments(createDocuments(eDocuments)); return sendEnvelope(envelopeDefinition, docuSignAccountId); }
What's Changed & Why?
- We added
signer.setCompany()andsigner.setTitle(): This fills the core signer profile details in the envelope's recipient list, ensuring the data is tied to the signer's identity. - We also added
templateRole.setCompany()andtemplateRole.setTitle(): This step is critical for mapping the values to your template's role-specific fields. If your template uses role-based field assignments, this ensures the right data goes to the right fields for each signer.
Quick Checks to Avoid Issues
- Make sure your
ESignerclass has getter methods forgetCompany()andgetTitle()(adjust the code if your signer data is stored elsewhere). - Double-check your DocuSign template: Confirm the Company/Title fields are assigned to the correct signer roles (e.g., fields linked to "signer1" will use values from the first
TemplateRolein your list).
内容的提问来源于stack exchange,提问作者Yupanqui Munoz

