Merge External Data Into Your Documents

Merge in External Data: XML Strings

To merge external data (in the form of an XML string) into an S-Docs document, you can create an S-Docs Job record and write the external data to the S-Docs Job record. You can then reference the XML data values as merge fields in your template.

For example, if you have {{!MyXXXField}} in your template and you write <MyXXXField>Tutorial</MyXXXField> to the S-Docs Job, you will see "Tutorial" in your generated document.

Nested XML Tags

This feature also supported nested XML tags, with the * character used as a delimiter between each level:

XML STRING
<aaa1>
    <bbb1>
        <ccc>test1</ccc>
    </bbb1>
    <bbb2>
        <ccc>test2</ccc>
    </bbb2>
</aaa1>
<aaa2>
    <bbb1>
        <ccc>test3</ccc>
    </bbb1>
    <bbb2>
        <ccc>test4</ccc>
    </bbb2>
</aaa2>


TEMPLATE
Field 1: {{!aaa1*bbb1*ccc}}
Field 2: {{!aaa1*bbb2*ccc}}
Field 3: {{!aaa2*bbb1*ccc}}
Field 4: {{!aaa2*bbb2*ccc}}


GENERATED DOCUMENT
Field 1: test1
Field 2: test2
Field 3: test3
Field 4: test4

Apex Code

You can also use this feature in your Apex code (for example, in an Apex class that does a webservice callout to retrieve the XML data string). In your Apex code, you can use the function SDBatch.writeRecordDataXML(SDJob__c sdjob, String xml) to write the XML string to an S-Docs Job record (this will store the XML string in the S-Docs Job's SDOC__RecordDataXML__c field, as well as the SDOC__RecordData2XML__c field if the string is too large to fit in the first field alone). For example:

String xmlData; // Your XML data goes here
SDOC__SDJob__c sdjob = new SDOC__SDJob__c(
    SDOC__ObjAPIName__c='Opportunity', // Object API Name
    SDOC__OID__c='006000000018HK6', // Object ID
    SDOC__Doclist__c='a15000000012GQ2', // Template ID
    SDOC__Start__c=true // Generate document upon insertion of S-Docs Job
);
SDBatch.writeRecordDataXML(sdjob, xmlData); // Write XML Data to S-Docs Job record
insert sdjob;

Given that you've configured your template to use merge fields referencing the XML data values, this code should generate a document that has your XML data merged into the specified locations.

Merge in External Table Data

You can also merge in external table data into your S-Docs documents. This involves formatting your external table data into an XML string and using Apex code to pass that string into an S-Docs Job record. This will allow the external table data to populate a table within an S-Docs document. Let's take a look at an example to see how this works.

Note: You can also merge XML table data into your documents synchronously via callable apex. Learn more here.

First, we built out a table within the S-Docs template editor that includes three columns -- one for product name, product description, and product code -- information that we'll assume is currently stored outside of Salesforce. Here's the source code for our table:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3Etable%0A%20%20.table499%20%7Bborder%3Asolid%20black%201px%3B%20border-collapse%3Acollapse%3B%20border-spacing%3A0px%3Bfont-family%3AArial%20Unicode%20MS%2CArial%2CHelvetica%2Csans-serif%3B%20font-size%3A10pt%3B%20width%3A100%25%20%7D%0A%20%20.table499header%20%7Btext-align%3Acenter%3Bfont-weight%3Abold%3Bborder%3Asolid%20black%201px%3Bcolor%3A%23FFFFFF%3Bbackground-color%3A%23000000%3B%7D%0A%20%20.table499footer%20%7Btext-align%3Aright%3Bfont-weight%3Abold%3Bborder%3Asolid%20black%201px%3B%20height%3A%2030px%7D%0A%20%20.table499RowEven%7Bborder%3Asolid%20black%201px%3B%7D%0A%20%20.table499RowOdd%7Bbackground-color%3A%23cdcdcd%3Bbackground-color%3A%23a8a8a8%3Bborder%3Asolid%20black%201px%3B%7D%0A%20%20.table499col0%2C%20.table499col1%2C%20.table499col2%20%7Bborder%3Asolid%20black%201px%3Btext-align%3Aleft%3Bfont-weight%3Anormal%3Bfont-size%3A12pt%3Bfont-family%3AArial%2C%20Helvetica%2C%20sans-serif%3B%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />
<table cellpadding="4" cellspacing="0" class="table499">
    <thead>
        <tr>
            <th class="table499header">Product Name</th>
            <th class="table499header">Product Description</th>
            <th class="table499header">Product Code</th>
        </tr>
    </thead>
    <tbody><!--{{!
      <LineItems>
      <class>table499</class>
      <listname externalList="true">products</listname>
      <column>productname</column>
      <column>productdescription</column>
      <column>productcode</column>
      </LineItems>
      }}-->
    </tbody>
</table>

As you can see, we've included some styling CSS within the <style> tags, and then defined our three table columns within <th> tags. Next, we added a LineItems statement within the <tbody> tags -- this statement will use your XML string to populate the table. Note that the <listname> tags include the externalList="true" attribute to denote that we will be merging in external data.

To ensure that our external table data will merge in correctly, we first need to format it into an XML string. You can write Apex code to format your table data into XML that follows the format below. Each product record includes one XML tag for each table column (in our example, 3 per record), and is enclosed within parent tags that have the same name as the <listname> in our S-Docs template.

<products>
  <productname>Apple</productname>
  <productdescription>Red fruit</productdescription>
  <productcode>4016</productcode>
</products>
<products>
  <productname>Banana</productname>
  <productdescription>Yellow fruit</productdescription>
  <productcode>4011</productcode>
</products>
<products>
  <productname>Pear</productname>
  <productdescription>Green fruit</productdescription>
  <productcode>3012</productcode>
</products>

Once your external table data is formatted correctly, the last step is to write Apex code similar to the following so that your XML string can be written to an S-Docs Job record and merged into your S-Docs template.

String lineItemDataXML = ''; // Your XML data goes here
SDOC__SDJob__c sdjob = new SDOC__SDJob__c(
    SDOC__ObjAPIName__c='Opportunity', //Object API name
    SDOC__OID__c='006000000018HK6', // Object ID
    SDOC__Doclist__c='a15000000012GQ2', //Template ID
    SDOC__Start__c='true' // Generate document upon insertion of S-Docs Job
);
SDOC.SDBatch.writeLineItemDataXML(sdjob, lineItemDataXML); // Write XML Data to S-Docs Job record
insert sdjob;

This code will generate a document with your XML data merged into the table. Using the data listed above, the generated document will look like this:

Tags: , ,

Was this helpful?