Skip to main content

DocumentRenderV2Command - Quick Reference Card

Basic Usage

var doc = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'template-name',
context: JSON.stringify({...}),
isPdfOutput: false
}
});

Template Syntax Cheat Sheet

SyntaxPurposeExample
{{var}}Simple variable{{customerName}}
{{obj.prop}}Nested property{{customer.address.city}}
{{#if var}}...{{/if}}Conditional{{#if approved}}Approved{{/if}}
{{#each arr}}...{{/each}}Loop{{#each items}}{{this.name}}{{/each}}
{{#arr}}...{{/arr}}Table repetitionSee table example below

Table Row Repetition

Row 1 (Header):  | Name | Amount | Status |
Row 2 (Marker): {{#items}}
Row 3 (Template): | {{name}} | ${{amount}} | {{status}} |
Row 4 (End): {{/items}}

Context Data Format

✅ Correct Format

context: JSON.stringify({
name: "John Doe",
amount: "1,234.56", // Pre-formatted
date: "Jan 17, 2026", // Pre-formatted
approved: true, // Boolean
items: [
{ id: 1, value: "Item 1" },
{ id: 2, value: "Item 2" }
]
})

❌ Wrong Format

context: {  // ❌ Don't pass object directly
amount: 1234.56, // ❌ Not formatted
date: new Date(), // ❌ Raw date object
approved: "yes" // ❌ String instead of boolean
}

Common Patterns

Pattern 1: Loan Agreement

var agreement = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'loan-agreement-template',
context: JSON.stringify({
customerName: context.firstName + ' ' + context.lastName,
loanAmount: $.format.number(context.amount, 2),
interestRate: context.rate,
monthlyPayment: $.format.number(context.payment, 2),
schedule: context.payments.map(p => ({
paymentNumber: p.number,
dueDate: $.format.date(p.date),
principal: $.format.number(p.principal, 2),
interest: $.format.number(p.interest, 2),
total: $.format.number(p.total, 2)
}))
}),
isPdfOutput: false
}
});

Pattern 2: Statement with Transactions

var statement = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'account-statement-template',
context: JSON.stringify({
accountNumber: context.accountNumber,
periodStart: $.format.date(context.startDate),
periodEnd: $.format.date(context.endDate),
openingBalance: $.format.number(context.opening, 2),
closingBalance: $.format.number(context.closing, 2),
transactions: context.txns.map(t => ({
date: $.format.date(t.transactionDate),
description: t.description,
amount: $.format.number(Math.abs(t.amount), 2),
isDebit: t.amount < 0,
isCredit: t.amount > 0,
balance: $.format.number(t.runningBalance, 2)
}))
}),
isPdfOutput: true
}
});

Pattern 3: Certificate

var cert = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'certificate-template',
context: JSON.stringify({
certificateNumber: context.certNumber,
issueDate: $.format.date(new Date()),
customerName: context.customerName,
amount: $.format.number(context.depositAmount, 2),
maturityDate: $.format.date(context.maturityDate),
interestRate: context.rate
}),
isPdfOutput: true
}
});

Error Handling

var result = doCmd('DocumentRenderV2Command', {
Data: {
documentName: 'template-name',
context: JSON.stringify(myData),
isPdfOutput: false
}
});

if (result.isSuccessful) {
// Success - result.data contains base64 document
context.documentBase64 = result.data;
} else {
// Error - check result.message
throw new Error('Document generation failed: ' + result.message);
}

Data Formatting Functions

// Numbers
$.format.number(1234.5678, 2) // "1,234.57"

// Dates
$.format.date(new Date()) // "01/17/2026"
$.format.date(new Date(), 'MM/dd/yyyy') // "01/17/2026"
$.format.date(new Date(), 'MMM dd, yyyy') // "Jan 17, 2026"

// Add days to date
$.addDays(new Date(), 30) // Date 30 days from now
$.addMonths(new Date(), 6) // Date 6 months from now

Conditional Logic in Context

// Prepare boolean flags in context
context: JSON.stringify({
hasCollateral: context.loanAmount > 500000,
requiresApproval: context.status === 'pending',
isOverdue: context.daysPastDue > 0,
showDetails: context.includeFullDetails === true
})

// Use in template
{{#if hasCollateral}}
COLLATERAL SECTION
{{/if}}

{{#if requiresApproval}}
PENDING APPROVAL
{{/if}}

Array Manipulation

// Filter, map, and format arrays
var filteredTransactions = context.transactions
.filter(t => t.amount > 0) // Only credits
.map(t => ({
date: $.format.date(t.date),
amount: $.format.number(t.amount, 2),
description: t.description.substring(0, 50) // Truncate
}));

context: JSON.stringify({
transactions: filteredTransactions
})

Template Testing Checklist

  • All placeholders have matching context variables
  • Numbers are pre-formatted with $.format.number()
  • Dates are pre-formatted with $.format.date()
  • Booleans are true/false (not strings)
  • Arrays are properly structured
  • Table markers are in separate rows
  • Conditional sections use boolean variables
  • Nested objects use dot notation correctly

Storage Patterns

Store in Context

// Generate document
var doc = doCmd('DocumentRenderV2Command', {...});

// Store base64 in context
if (doc.isSuccessful) {
context.generatedDocument = doc.data;
context.documentGenerated = true;
context.documentGeneratedDate = new Date().toISOString();
}

Store in Database

// Generate document
var doc = doCmd('DocumentRenderV2Command', {...});

// Save to UserDocuments table
if (doc.isSuccessful) {
doCmd('CreateLoanUserDocumentCommand', {
Data: {
loanId: context.loanId,
documentType: 'loan-agreement',
content: doc.data, // base64 string
fileName: 'loan-agreement-' + context.loanNumber + '.docx'
}
});
}

Common Errors and Solutions

ErrorCauseSolution
"Template not found"Wrong documentNameCheck template name in database
Placeholder not replacedVariable name mismatchVerify context contains variable
"Invalid JSON"context not stringifiedUse JSON.stringify()
Table not repeatingMarkers in wrong rowsPut markers in separate rows
Conditional always showsNot booleanUse true/false, not "yes"/"no"

Performance Tips

  1. Format data once: Don't format same data multiple times
  2. Filter arrays early: Reduce array size before passing to template
  3. Limit table rows: Large tables (>1000 rows) may be slow
  4. Test with real data: Always test with production-like data volumes
  5. Cache templates: Templates are cached automatically by name

Print this page for quick reference while developing!