'Docusign embedded sigin - Merge field is not working

I am using embedding signing from salesforce using apex toolkit. I could not merge fields from salesforce. I am trying to prepopulate firstname and lastname. I tried with customtabs and customfields. Custom tab is repeating the same value for all the tabs and custom field is not working. Can someone help pls.

contact c =[select firstName,lastname, Envelope_Id__c from contact where id = :currentUser.contactId]; 
    
    string EnvelopeId='';
      Id mySourceId = currentUser.ContactId; // The ID of the initiating Salesforce object
      String conStr = (String) currentUser.ContactId + '~Contact';
      ContractType__mdt ContractType= [SELECT label, Envelope_Configuration__c,External_Document_Id__c
      FROM ContractType__mdt where Envelope_Configuration__c =:application.Envelope_Configuration__c limit 1];
     string templateId= ContractType.External_Document_Id__c;
     /*
      dfsle.Envelope dsEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
          new dfsle.Entity(mySourceId)) // The initiating Salesforce entity--current SF user (salesperson)
          .withDocuments(new List<dfsle.Document> {
          dfsle.Document.fromTemplate(dfsle.UUID.parse(templateId), description)
          })
          .withRecipients(new List<dfsle.Recipient> {
          dfsle.Recipient.newEmbeddedSigner() // An embedded signer
          }
      );
      if(!Test.isRunningTest()){
      // Send the envelope.
      dsEnvelope = dfsle.EnvelopeService.sendEnvelope(
          dsEnvelope, // The envelope to send
          true // Send now?
      );

      
       EnvelopeId= String.valueOf(dsEnvelope.docuSignId);
      
  }*/


  dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
  new dfsle.Entity(mySourceId));
// The initiating Salesforce entity (an opportunity).


dfsle.Tab myTextFNAMETab = new dfsle.TextTab()
     .withRequired(true) // Signer must enter value
     .withValue(c.firstName)
     .withReadOnly(false)
     .withName('FName')
    // .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4)); 
    .withPosition(new dfsle.Tab.Position(
      1, // The document to use
      1, // Page number on the document
      149, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height

      dfsle.Tab myTextLNAMETab = new dfsle.TextTab()
     .withRequired(true) // Signer must enter value
     .withValue(c.lastName)
     .withReadOnly(false)
     .withName('LName')
    // .withAnchor(new dfsle.Tab.Anchor('Legal Name', true, true, null, true, true, 'pixels', 60, -4)); 
    .withPosition(new dfsle.Tab.Position( 
      1, // The document to use
      1, // Page number on the document
      230, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height

     dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
     myRecipient2.withTabs(new List<dfsle.Tab> {myTextLNAMETab});

  //dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
//add Recipient to the Envelope
myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient2});
myEnvelope = myEnvelope.withEmail('Testt Email Subject', 'Test Email Message');

//myTemplateId contains the DocuSign Id of the DocuSign Template
dfsle.UUID myTemplateId = dfsle.UUID.parse(templateId);

//create a new document for the Envelope
dfsle.Document myDocument = dfsle.Document.fromTemplate(
  myTemplateId, // templateId in dfsle.UUID format
  'Enrollment Agreement'); // name of the template
  dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);
//add document to the Envelope
myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> { myDocument })
.withCustomFields(new List<dfsle.CustomField> {myField});
if(!Test.isRunningTest()){
  myEnvelope = dfsle.EnvelopeService.sendEnvelope(
  myEnvelope, // The envelope to send
  true); // Send now?


Solution 1:[1]

I'm assuming you're trying to populate existing fields in your template, if that's the case:

// This will populate all the fields with data label name 'FName':

dfsle.Tab prefill_firstname = new dfsle.TextTab()
.withValue(myContact.firstName)
.withDataLabel('FName');

// This will populate all the fields with data label name 'LName':

dfsle.Tab prefill_lastname = new dfsle.TextTab()
    .withValue(myContact.lastName)
    .withDataLabel('LName');

// Otherwise, if you just want to a create new recipient's first & last name tabs instead of populating field:

dfsle.Tab New_firstname_tab = new dfsle.FirstNameTab()
      .withName('AdditionLastName')
      .withPosition(new dfsle.Tab.Position( 
      1, // The document to use
      1, // Page number on the document
      400, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height

dfsle.Tab New_lastname_tab = new dfsle.LastNameTab()
      .withName('AdditionLastName')
      .withPosition(new dfsle.Tab.Position( 
      1, // The document to use
      1, // Page number on the document
      500, // X position
      240, // Y position
      80, // 100 pixels wide
      null)); // Default height


dfsle.Recipient myRecipient2 = dfsle.Recipient.newEmbeddedSigner();
  myRecipient2.withTabs(new List<dfsle.Tab> {prefill_firstname,prefill_lastname,New_firstname_tab,New_lastname_tab});
  myRecipient2.withRole('Manager');  // remember to specify the role that match the Role in your template.

As for the envelope Custom Fields:

dfsle.CustomField myField = new dfsle.CustomField ('text', 'DSFSSourceObjectId', conStr, null, false, false);

Note: Envelope Custom Fields aren't directly visible to a recipient during a signing session, since you have set the show parameter to 'false', it will not be included in the Certificate of Completion. However, it should still be visible through web console reporting and API: https://www.docusign.com/blog/dsdev-trenches-tabs-and-custom-fields

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joseph