Skip to content

Cookbook: Build an AI-Powered Invoice Processor in 10 Minutes

In the world of enterprise software, automating document processing is notoriously difficult. A seemingly simple task like extracting data from a PDF invoice often becomes a multi-month project involving expensive third-party OCR services, fragile API integrations, and thousands of lines of custom code.

On the Simple Platform, it's a 10-minute task.

This guide will walk you through building a complete, production-ready, AI-powered invoice processing workflow. By the end, you will have an application that can automatically read an uploaded invoice PDF and populate your form fields with structured, validated data.

Prerequisites: None. All you need is access to the Simple Platform.


Step 1: Define Your Data Model (2 Minutes)

First, we'll define the data structure for our invoices in a Simple Configuration Language (SCL) file. We will use a document field for the PDF upload and a powerful json field to store the extracted line items. This modern approach gives us the flexibility of a document database with the power of a relational system.

Create your tables.scl file:

ruby
# file: tables.scl

table invoice, invoices {
  default :id, :timestamps, :userstamps

  # The field for the uploaded invoice PDF
  required invoice, :document {
    allowed_types "application/pdf"
  }

  # Fields to be populated by our AI script
  optional invoice_number, :string
  optional vendor_name, :string
  optional total_amount, :decimal

  # A JSON field to store a structured array of line items.
  # Simple Platform's search can index and query inside this field.
  optional line_items, :json
}

Action: Save this file. The platform will automatically provision your database and update your GraphQL API.


Step 2: Configure the Record Behavior (1 Minute)

Next, we need to tell the platform to run our automation logic whenever an invoice record is handled.

  1. Navigate to System > Settings > Record Behaviors.
  2. Click New to create a new behavior.
  3. For the Table field, select your newly created invoice table.
  4. Ensure Is Active is checked.
  5. Proceed to the next step to write the script.

Step 3: Write the Automation Logic (5 Minutes)

This is the core of our solution. Copy and paste the following JavaScript code into the Script field of your new Record Behavior. This script will run securely on the server, orchestrating the entire AI extraction process.

The Record Behavior Script:

javascript
// file: invoice_behavior.js

export default async ({ $ai, $form }) => {
  // We only want this logic to run when a user uploads a new file in the UI.
  if ($form.event !== 'update' || !$form.updated('invoice')) {
    return
  }

  const invoiceHandle = $form('invoice').value()

  // If the user clears the file, we do nothing.
  if (!invoiceHandle) {
    return
  }

  // Define the exact JSON structure we want the AI to return.
  // This schema is our guarantee of a predictable, validated output.
  const schema = {
    properties: {
      invoice_number: { description: 'The unique invoice number.', type: 'string' },
      line_items: {
        description: 'A list of all individual line items from the invoice.',
        items: {
          properties: {
            description: { type: 'string' },
            quantity: { type: 'integer' },
            unit_price: { type: 'number' }
          },
          required: ['description', 'quantity', 'unit_price'],
          type: 'object'
        },
        type: 'array'
      },
      total_amount: { description: 'The final, grand total amount due.', type: 'number' },
      vendor_name: { description: 'The name of the company that issued the invoice.', type: 'string' }
    },
    required: ['invoice_number', 'vendor_name', 'total_amount'],
    type: 'object'
  }

  try {
    // Provide instant feedback to the user while the AI works its magic.
    $form('invoice').info('AI is analyzing your invoice...')

    // This is the magic. A single, secure call to the AI Engine.
    const { data: extractedData } = await $ai.extract(invoiceHandle, {
      model: 'large', // Use a powerful model for complex document parsing.
      prompt: 'Extract all key details and line items from this invoice document. Ensure the data strictly follows the provided schema.',
      schema
    })

    // Populate the form with the guaranteed, structured JSON data.
    $form('invoice_number').set(extractedData.invoice_number)
    $form('vendor_name').set(extractedData.vendor_name)
    $form('total_amount').set(extractedData.total_amount)
    $form('line_items').set(extractedData.line_items)

    $form('invoice').info('✅ Document analysis complete!')
  }
  catch (error) {
    $form('invoice').error(`AI processing failed: ${error.message}`)
  }
}

Action: Save the Record Behavior.


Step 4: See It in Action (2 Minutes)

Your AI-powered workflow is now live.

  1. Navigate to your Invoices table in the UI.
  2. Click New.
  3. Drag and drop any PDF invoice onto the Invoice field.
  4. Watch. Within seconds, the Invoice Number, Vendor Name, Total Amount, and Line Items fields will populate automatically with the extracted data.

The Simple Advantage: The 10-Minute Workflow

You have just built a secure, scalable, AI-powered document processing workflow. Compare this to the "standard" enterprise approach:

RequirementThe Legacy Way (Salesforce/SAP)The Simple Way
Data ModelClick through wizards or write migration scripts.10 lines of SCL.
OCR ServiceEvaluate vendors, buy licenses, manage API keys.Built-in $ai primitive.
Backend LogicWrite Apex triggers, HTTP callout classes, JSON parsers, and test mocks (100+ lines).15 lines of JavaScript.
File SecurityBuild custom middleware to securely transfer files to the OCR provider.Secure Asset Federation (Automatic).
Time to ValueWeeks or Months.10 Minutes.

This is the power of an AI-native, developer-centric platform. This is the future of enterprise software.


Next Steps