# Build a custom Apex data provider A provider prepares the dataset before Portwood merges it. Use one for calculated values, deliberately shaped reports, or data that the visual query builder cannot express. #### 1. Implement both required methods ```apex global interface DocGenDataProvider { Map getData(Id recordId); List getFieldNames(); } ``` Your class implements `portwoodglobal.DocGenDataProvider`; expose both methods as global. `getData()` supplies values and `getFieldNames()` supplies the authoring field list. The old one-method example was incomplete. Use the complete [KBAccountProvider class](https://github.com/Portwood-Global-Solutions/Portwood/blob/main/docs/knowledgebase/examples/apex/KBAccountProvider.cls) and its [metadata file](https://github.com/Portwood-Global-Solutions/Portwood/blob/main/docs/knowledgebase/examples/apex/KBAccountProvider.cls-meta.xml). It uses `with sharing` and `WITH USER_MODE`, selects standard Account/Contact fields, limits the Contact query to 100, and implements both methods. The ContactCount is the count of those returned rows, not a claim to count unlimited Contacts. #### 2. Understand the data shape ```apex Map data = new Map{ 'Name' => 'Northstar Bikes', 'Owner' => new Map{'Name' => 'Practice Owner'}, 'Contacts' => new Map{ 'records' => new List{ new Map{'Name' => 'Avery Chen', 'Email' => 'avery@example.com'} } } }; ``` | Data | Template | | --------------------------------- | --------------------------------------- | | Root scalar | `{Name}` | | Nested map | `{Owner.Name}` | | Relationship wrapper with records | `{#Contacts}{Name}: {Email}{/Contacts}` | Inside the loop, `Name` is the Contact's key. Include parent paths and list field paths in `getFieldNames()` so an author can discover them. Keep shape and casing stable as your provider evolves. #### 3. Bind the template Choose **Apex Class (Data Provider)** as its data source and enter `KBAccountProvider`, or set the equivalent Query Config: ```json { "v": 4, "provider": "KBAccountProvider" } ``` Save and activate a version, select a valid sample Account, and preview. In the example, the preview returned Northstar Bikes and two Contacts. With your own sample record, compare the Account name and Contact rows with the record in Salesforce. #### 4. Test your own boundary cases Test an inaccessible record/field, no related rows, null values, many rows, and the intended Flow/bulk path. Keep data selection separate from layout so a template change does not silently widen access. A provider is responsible for sharing, object/field permissions, and any external integration policy. #### Use a map without a provider If your calling process already has the data, use `generatePdfBlobFromData` or `generateAndSaveFromData`. The same root/map/records shape applies. You do not need to insert temporary Salesforce records just to merge calculated values. --- Related articles - [Use prepared or custom data](https://portwood.dev/knowledgebase/custom-data.md) - [Generate documents from your own Apex](https://portwood.dev/knowledgebase/apex-generation.md) - [Loops, row numbers, grouping, and scope](https://portwood.dev/knowledgebase/tag-loops.md) [Browse the article directory](https://portwood.dev/knowledgebase/)