Documentation

Example App


This page walks through one complete, working Custom App, a small petty cash logger. Paste it directly into a new Custom App's UI Code editor and click Open App to see it run against your own accounts.


It is deliberately simple, but it touches the three things almost every Custom App needs: reading your accounts, posting a transaction, and remembering something between sessions using the app's own storage. Read Custom App Structure & Development Workflow first if you have not built a Custom App before, and keep the App Context guide open alongside this page for the full reference on every method used below.


What it does


The app shows a small form with a description, an amount, and two account dropdowns, one for the expense being recorded and one for the cash or bank account the money came from. Submitting the form posts a balanced journal entry: a debit to the expense account and a credit to the cash account.


To save a little time on repeat use, the app also remembers the two accounts you picked last time, using its own storage, and pre-selects them the next time you open it. This is a good example of what an app's storage is for.


The transaction itself is the only accounting fact, and it is posted through postTransaction, not stored a second time inside the app.


The code


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Petty Cash Logger</title>
<style>
  body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; padding: 24px; color: #212529; background: #fff; }
  h1 { font-size: 1.2rem; margin: 0 0 4px; }
  p.sub { color: #6c757d; font-size: .85rem; margin: 0 0 20px; }
  label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: 4px; margin-top: 14px; }
  input, select { width: 100%; padding: 8px 10px; font-size: .9rem; border: 1px solid #ced4da; border-radius: 4px; box-sizing: border-box; }
  button { margin-top: 20px; width: 100%; padding: 10px; font-size: .9rem; font-weight: 600; border: none; border-radius: 4px; background: #212529; color: #fff; cursor: pointer; }
  button:disabled { opacity: .6; cursor: default; }
  #message { margin-top: 14px; font-size: .85rem; padding: 8px 10px; border-radius: 4px; display: none; }
  #message.success { display: block; background: #eafaf0; color: #1a6b3c; }
  #message.error { display: block; background: #fdecea; color: #b91c1c; }
</style>
</head>
<body>

  <h1>Petty Cash Logger</h1>
  <p class="sub">Record a small cash expense in a couple of clicks.</p>

  <form id="entryForm">
    <label for="description">Description</label>
    <input type="text" id="description" placeholder="e.g. Taxi fare" required>

    <label for="amount">Amount</label>
    <input type="number" id="amount" step="0.01" min="0.01" required>

    <label for="expenseAccount">Expense account (debit)</label>
    <select id="expenseAccount" required></select>

    <label for="cashAccount">Paid from (credit)</label>
    <select id="cashAccount" required></select>

    <button type="submit" id="submitBtn">Record Expense</button>
  </form>

  <div id="message"></div>

<script>
(async () => {
  const form = document.getElementById('entryForm');
  const descriptionInput = document.getElementById('description');
  const amountInput = document.getElementById('amount');
  const expenseSelect = document.getElementById('expenseAccount');
  const cashSelect = document.getElementById('cashAccount');
  const submitBtn = document.getElementById('submitBtn');
  const messageBox = document.getElementById('message');

  function showMessage(text, type) {
    messageBox.textContent = text;
    messageBox.className = type;
  }

  function populateSelect(select, accounts, placeholder) {
    select.innerHTML = '';
    const emptyOption = document.createElement('option');
    emptyOption.value = '';
    emptyOption.textContent = placeholder;
    select.appendChild(emptyOption);
    for (const acc of accounts) {
      const option = document.createElement('option');
      option.value = acc.id;
      option.textContent = acc.name;
      select.appendChild(option);
    }
  }

  // Load accounts and split them by type for the two dropdowns.
  let accounts = [];
  try {
    accounts = await AppContext.getAccounts();
  } catch (err) {
    showMessage('Could not load accounts: ' + err.message, 'error');
    return;
  }

  const expenseAccounts = accounts.filter(a => a.type === 'expense');
  const cashAccounts = accounts.filter(a => a.subType === 'cash');

  populateSelect(expenseSelect, expenseAccounts, 'Select an expense account');
  populateSelect(cashSelect, cashAccounts, 'Select a cash or bank account');

  // Restore the accounts used last time, if any were saved.
  try {
    const saved = await AppContext.getData();
    if (saved && saved.lastExpenseAccount) expenseSelect.value = saved.lastExpenseAccount;
    if (saved && saved.lastCashAccount) cashSelect.value = saved.lastCashAccount;
  } catch (err) {
    // No stored data yet on first run; nothing to restore.
  }

  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    showMessage('', '');

    const description = descriptionInput.value.trim();
    const amount = parseFloat(amountInput.value);
    const expenseAccountId = expenseSelect.value;
    const cashAccountId = cashSelect.value;

    if (!description || !amount || amount <= 0 || !expenseAccountId || !cashAccountId) {
      showMessage('Please fill in every field with a valid amount.', 'error');
      return;
    }

    submitBtn.disabled = true;
    submitBtn.textContent = 'Recording…';

    const today = new Date();

    try {
      await AppContext.postTransaction(
        today.getDate(),
        today.getMonth() + 1,
        today.getFullYear(),
        description,
        [{ accountID: Number(expenseAccountId), amount }],
        [{ accountID: Number(cashAccountId), amount }]
      );

      // Remember these accounts as a convenience for next time.
      // This is a preference, not accounting data, so it belongs
      // in the app's own storage rather than the ledger.
      await AppContext.saveData({
        lastExpenseAccount: expenseAccountId,
        lastCashAccount: cashAccountId
      });

      showMessage('Expense recorded.', 'success');
      form.reset();
      expenseSelect.value = expenseAccountId;
      cashSelect.value = cashAccountId;
    } catch (err) {
      showMessage('Could not record the expense: ' + err.message, 'error');
    } finally {
      submitBtn.disabled = false;
      submitBtn.textContent = 'Record Expense';
    }
  });
})();
</script>

</body>
</html>

Where each AppContext call is used


×
Call Used for
getAccounts() Populating the two account dropdowns when the app loads.
getData() Restoring the accounts used last time, if this app has been run before.
postTransaction() Posting the actual balanced journal entry when the form is submitted.