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 jsonb 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_upload, :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, :jsonb
}

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 ({ $form, $ai }) => {
  // We only want this logic to run when a user uploads a new file in the UI.
  if ($form.event !== 'update' || !$form.updated('invoice_upload')) {
    return;
  }

  const invoiceHandle = $form('invoice_upload').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 = {
    type: "object",
    properties: {
      invoice_number: { type: "string", description: "The unique invoice number." },
      vendor_name: { type: "string", description: "The name of the company that issued the invoice." },
      total_amount: { type: "number", description: "The final, grand total amount due." },
      line_items: {
        type: "array",
        description: "A list of all individual line items from the invoice.",
        items: {
          type: "object",
          properties: {
            description: { type: "string" },
            quantity: { type: "integer" },
            unit_price: { type: "number" }
          },
          required: ["description", "quantity", "unit_price"]
        }
      }
    },
    required: ["invoice_number", "vendor_name", "total_amount"]
  };

  try {
    // Provide instant feedback to the user while the AI works its magic.
    $form('invoice_upload').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, {
      prompt: "Extract all key details and line items from this invoice document. Ensure the data strictly follows the provided schema.",
      model: 'large', // Use a powerful model for complex document parsing.
      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_upload').info("✅ Document analysis complete!");

  } catch (error) {
    $form('invoice_upload').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 Upload 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.

On legacy platforms like Salesforce or SAP, this is a multi-month project requiring specialized consultants, expensive third-party AppExchange packages, complex Apex/ABAP code, and fragile API integrations.

On Simple, it's two declarative files and a few minutes of your time. This is the power of an AI-native, developer-centric platform. This is the future of enterprise software.


Next Steps