{"id":276,"date":"2024-11-28T15:22:36","date_gmt":"2024-11-28T15:22:36","guid":{"rendered":"https:\/\/docu2.me\/help\/?p=276"},"modified":"2025-04-07T01:15:28","modified_gmt":"2025-04-06T22:15:28","slug":"listing-accounts-with-their-respective-contacts-using-callable","status":"publish","type":"post","link":"https:\/\/docu2.me\/help\/listing-accounts-with-their-respective-contacts-using-callable\/","title":{"rendered":"Listing Accounts with Their Respective Contacts using Callable"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Objective<\/h3>\n\n\n\n<p>Create a table list that displays Salesforce Account information with their respective Contacts, using the Callable feature in docu2.me.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>This example demonstrates the use of Callable, which allows developers to use a common interface to create loosely coupled integrations between Apex classes or triggers, even for code in separate packages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step-by-Step Guide<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating a New Document in docu2.me<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open docu2.me and create a new document<\/li>\n\n\n\n<li>Define that we will be working with the Account Object, which is related to the Contact Object<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docu2.me\/help\/wp-content\/uploads\/2024\/11\/resource-manager-callable.gif\" alt=\"Creating a new document in docu2.me and selecting resource manager\"\/><figcaption class=\"wp-element-caption\">Creating a new document and accessing Resource Manager<\/figcaption><\/figure>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Creating the Apex Callable Class<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Create a new Apex Class in your Salesforce Organization or in your preferred code editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global with sharing class CallableSample implements Callable {\n    global Object call(String action, Map&lt;String,Object&gt; args) {\n        if (action.equalsIgnoreCase('generateAccountsWithContacts')) {\n            return generateAccountsWithContacts(args);\n        }\n        return 'Action not implemented: ' + action;\n    }\n    private String generateAccountsWithContacts(Map&lt;String,Object&gt; args) {\n        try {\n            Integer accountLimit = Integer.valueOf(args.get('AccountLimit') ?? '10');\n            Integer contactLimit = Integer.valueOf(args.get('ContactLimit') ?? '5');\n            List&lt;Account&gt; accounts = &#91;SELECT Name, Website,\n                                      (SELECT Name, Title, Phone, Email FROM Contacts LIMIT :contactLimit)\n                                      FROM Account\n                                      LIMIT :accountLimit];\n            return generateHTMLTable(accounts, contactLimit);\n        } catch (Exception e) {\n            return 'Error generating table: ' + e.getMessage();\n        }\n    }\n    private String generateHTMLTable(List&lt;Account&gt; accounts, Integer contactLimit) {\n        List&lt;String&gt; rows = new List&lt;String&gt;{\n            '&lt;table border=\"1\" style=\"border-collapse: collapse; margin: auto; width: auto;\"&gt;'\n        };\n        for (Account acc : accounts) {\n            rows.add('&lt;tr style=\"background-color: #f2f2f2;\"&gt;&lt;td style=\"padding: 8px; font-weight: bold;\"&gt;' +\n                acc.Name + (String.isNotBlank(acc.Website) ? ' - ' + acc.Website : '') + '&lt;\/td&gt;&lt;\/tr&gt;');\n            rows.add('&lt;tr&gt;&lt;td style=\"padding: 8px;\"&gt;&lt;table style=\"width: 100%; border-collapse: collapse;\"&gt;');\n            rows.add('&lt;tr style=\"background-color: #e6e6e6;\"&gt;&lt;th style=\"padding: 4px; border: 1px solid #ddd;\"&gt;Name&lt;\/th&gt;' +\n                '&lt;th style=\"padding: 4px; border: 1px solid #ddd;\"&gt;Title&lt;\/th&gt;' +\n                '&lt;th style=\"padding: 4px; border: 1px solid #ddd;\"&gt;Phone&lt;\/th&gt;' +\n                '&lt;th style=\"padding: 4px; border: 1px solid #ddd;\"&gt;Email&lt;\/th&gt;&lt;\/tr&gt;');\n            if (!acc.Contacts.isEmpty()) {\n                for (Contact con : acc.Contacts) {\n                    rows.add('&lt;tr&gt;&lt;td style=\"padding: 4px; border: 1px solid #ddd;\"&gt;' + (con.Name ?? '') + '&lt;\/td&gt;' +\n                        '&lt;td style=\"padding: 4px; border: 1px solid #ddd;\"&gt;' + (con.Title ?? '') + '&lt;\/td&gt;' +\n                        '&lt;td style=\"padding: 4px; border: 1px solid #ddd;\"&gt;' + (con.Phone ?? '') + '&lt;\/td&gt;' +\n                        '&lt;td style=\"padding: 4px; border: 1px solid #ddd;\"&gt;' + (con.Email ?? '') + '&lt;\/td&gt;&lt;\/tr&gt;');\n                }\n                if (acc.Contacts.size() == contactLimit) {\n                    rows.add('&lt;tr&gt;&lt;td colspan=\"4\" style=\"padding: 4px; text-align: center; font-style: italic;\"&gt;' +\n                        '... and possibly more contacts&lt;\/td&gt;&lt;\/tr&gt;');\n                }\n            } else {\n                rows.add('&lt;tr&gt;&lt;td colspan=\"4\" style=\"padding: 4px; text-align: center; font-style: italic;\"&gt;' +\n                    'No contacts&lt;\/td&gt;&lt;\/tr&gt;');\n            }\n            rows.add('&lt;\/table&gt;&lt;\/td&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;td style=\"padding: 8px;\"&gt;&lt;\/td&gt;&lt;\/tr&gt;');\n        }\n        rows.add('&lt;\/table&gt;');\n        return String.join(rows, '');\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Main Components of the Class:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Implementation of Callable Interface:<\/strong> Handles the received action and arguments<\/li>\n\n\n\n<li><strong>Retrieval of Account and Contact Data:<\/strong> Fetches accounts with their related contacts<\/li>\n\n\n\n<li><strong>HTML Table Generation:<\/strong> Creates a formatted HTML table with styling for visual presentation<\/li>\n<\/ul>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Configuring the Callable in docu2.me<\/strong><\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the docu2.me document, right-click and choose &#8220;Resource Manager&#8221;<\/li>\n\n\n\n<li>Select &#8220;New Resource&#8221;<\/li>\n\n\n\n<li>Configure the resource:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Resource Type: &#8220;Callable&#8221;<\/li>\n\n\n\n<li>Callable API Name: &#8220;generateAccountsWithContacts&#8221;<\/li>\n\n\n\n<li>Salesforce Apex Class: &#8220;CallableSample&#8221;<\/li>\n<\/ul>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Add the arguments:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Key:<\/strong> AccountLimit | <strong>Value<\/strong>: 3<\/li>\n\n\n\n<li><strong>Key:<\/strong> ContactLimit | <strong>Value:<\/strong> 2<\/li>\n<\/ul>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>Save the settings<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docu2.me\/help\/wp-content\/uploads\/2024\/11\/resource-manager-callable-created.gif\" alt=\"Configuring and creating the Callable resource in the Resource Manager\"\/><figcaption class=\"wp-element-caption\">Configuring the Callable resource with arguments<\/figcaption><\/figure>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Inserting the Callable into the Document<\/strong><\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Right-click in the document<\/li>\n\n\n\n<li>Choose &#8220;Dynamic Fields&#8221;<\/li>\n\n\n\n<li>In &#8220;Select a resource from global&#8221;, navigate to:<\/li>\n<\/ol>\n\n\n\n<p>Document Resource &gt; Local Callable Apex &gt; generateAccountsWithContacts<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docu2.me\/help\/wp-content\/uploads\/2024\/11\/resource-manager-callable-insert.gif\" alt=\"Inserting the Callable resource into the document as a dynamic field\"\/><figcaption class=\"wp-element-caption\">Inserting the Callable dynamic field into the document<\/figcaption><\/figure>\n\n\n\n<p>Insert the dynamic field: {!$Callable.generateAccountsWithContacts}<\/p>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Preview and Testing<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Use the preview or print button to view the result.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docu2.me\/help\/wp-content\/uploads\/2024\/11\/resource-manager-callable-print.gif\" alt=\"Previewing the document with the Callable results\"\/><figcaption class=\"wp-element-caption\">Previewing the document with generated Account and Contact data<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Expected Result<\/h3>\n\n\n\n<p>The generated HTML table will display:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docu2.me\/help\/wp-content\/uploads\/2024\/11\/resource-manager-callable-print.png\" alt=\"Final output showing Accounts and their associated Contacts in a formatted table\"\/><figcaption class=\"wp-element-caption\">Final output of the Account and Contact table<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account name and website in the header<\/li>\n\n\n\n<li>Contact details in a nested table including:\n<ul class=\"wp-block-list\">\n<li>Name<\/li>\n\n\n\n<li>Title<\/li>\n\n\n\n<li>Phone<\/li>\n\n\n\n<li>Email<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Output<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account 1\n<ul class=\"wp-block-list\">\n<li>Contact 1<\/li>\n\n\n\n<li>Contact 2<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>&#8230; and possibly more contacts<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account 2\n<ul class=\"wp-block-list\">\n<li>Contact 1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Account 3\n<ul class=\"wp-block-list\">\n<li>Contact 1<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important Considerations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Customization:<\/strong> The limits for accounts (3) and contacts (2) can be adjusted as needed<\/li>\n\n\n\n<li><strong>Performance:<\/strong> Consider the data volume when setting limits<\/li>\n\n\n\n<li><strong>Error Handling:<\/strong> The class includes basic error handling<\/li>\n\n\n\n<li><strong>Styling:<\/strong> The HTML table includes basic styles for better presentation<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Setting Limits:<\/strong> Balance details and performance based on your org&#8217;s data volume<\/li>\n\n\n\n<li><strong>Error Handling:<\/strong> Always check the returned value and consider implementing additional error logs<\/li>\n\n\n\n<li><strong>Performance Considerations:<\/strong> Monitor performance with larger datasets and consider implementing caching if necessary<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How to Use<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Basic Implementation<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Action: generateAccountsWithContacts<\/li>\n\n\n\n<li>Arguments:\n<ul class=\"wp-block-list\">\n<li>AccountLimit: 10<\/li>\n\n\n\n<li>ContactLimit: 5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Default Settings (10 Accounts, 5 Contacts each)<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Action: generateAccountsWithContacts<\/li>\n\n\n\n<li>Arguments: {}<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Custom Limits<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Action: generateAccountsWithContacts<\/li>\n\n\n\n<li>Arguments:\n<ul class=\"wp-block-list\">\n<li>AccountLimit: 3<\/li>\n\n\n\n<li>ContactLimit: 2<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Maximum Display<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Action: generateAccountsWithContacts<\/li>\n\n\n\n<li>Arguments:\n<ul class=\"wp-block-list\">\n<li>AccountLimit: 50<\/li>\n\n\n\n<li>ContactLimit: 10<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Apex Implementation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CallableSample sample = new CallableSample();\nMap&lt;String,Object&gt; args = new Map&lt;String,Object&gt;{\n    'AccountLimit' =&gt; '5',\n    'ContactLimit' =&gt; '3'\n};\nString result = (String)sample.call('generateAccountsWithContacts', args);<\/code><\/pre>\n\n\n\n<p>This example demonstrates the advanced integration between docu2.me and Salesforce, allowing the creation of dynamic and customized documents with related Account and Contact data. to explaining the Filter Block feature in docu2.me. It guides the user through the process step-by-step and highlights the key points of the feature&#8217;s behavior and usage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use docu2.me&#8217;s Callable feature to display Salesforce Accounts with their Contacts in formatted HTML tables, combining Apex integration with dynamic document generation capabilities.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","inline_featured_image":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-dynamic-resources"],"_links":{"self":[{"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":4,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/posts\/276\/revisions\/723"}],"wp:attachment":[{"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docu2.me\/help\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}