salesforcedocusignapiapex

Send Merge Field data in new DocuSign Apex Toolkit


Do you know how to send data to custom field (salesforce merge field) using new Apex Toolkit Docusign API?

I've been following this tutorial: salesforce-sending-signing-template

But there are not examples about filling custom fields with salesforce data. I tried to use custom field class, but it has only read properties.

How can you send salesforce data to template document?


Solution

  • public class SampleMergeFields {

     public static void sendMergeFieldTemplate() { 
    
         //create recipient
         dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
                                         'XXXXX', //name of recipient
                                         'xxxx.xx@xxx.com', //email of the recipient
                                         null,
                                         'Signer 1', //match role value defined in the template
                                         new dfsle.Entity(UserInfo.getUserId())); //pass in the ID of the source object here
    
        //create document                                 
        dfsle.UUID myTemplateId = dfsle.UUID.parse('XXXXX-XXXX-XX'); //Docusign template Id
        dfsle.Document myDocument =dfsle.Document.fromTemplate(
                                            myTemplateId, // templateId in dfsle.UUID format
                                            'myTemplate');
    
        //create custom field
         dfsle.CustomField myCustomField1 = new dfsle.CustomField(
                                                    'text', //type
                                                    '##SFStudent__c', //##SF+Salesforce API name of the object                                                    
                                                    'a0G5A00000TbNxVUAV', //Id of the record                                          
                                                    null,
                                                    true,
                                                    true);
    
       dfsle.Envelope myEnvelope = new dfsle.Envelope(
                                   null,
                                   null,
                                   null,
                                   null,
                                   new List<dfsle.Document> { myDocument },
                                   null,
                                   new List<dfsle.CustomField> { myCustomField1 },
                                   null,
                                   'Hello from DocuSign',
                                   'My Message',
                                   null,
                                   null);
       myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });
       // Send the envelope
       myEnvelope = dfsle.EnvelopeService.sendEnvelope(
                                            myEnvelope, // The envelope to send
                                            true); // Send now?
    
     } 
    

    }

    Hello Rene,
    Using the above code you would be able to set merge fields data for the Salesforce merge fields defined in your DocuSign template using the Apex toolkit.

    Note, that you have to pass RecordID of the Salesforce.com record that the template should pull up values from in the Custom field named '##SFStudent__c'. For instance if you have an custom object Student__c in your org having two fields a1__c and a2__c and considering both these fields are present on a template you are using for sending. The name of the custom field in this case should be '##SF'+API Name of the object and the value of the custom field should be Salesforce.com Id of the record.

    You can define merge fields from multiple Salesforce.com objects as well. In that case you will have to define additional custom fields for the objects. Say you have a template which contains fields from Account as well as Contact. In that case you will have to create 2 custom fields named '##SFAccount' and '##SFContact' and populate their values accordingly.

    Please note that Salesforce connect also needs to defined in the DocuSign account which you are using and should be pointing to the Salesforce.com org that has the toolkit installed in order for this to work correctly.

    Can you try this at your end and check ?