Skip to main content

How-to: Parse invoices using Airtable's AI


Mike_AutomaticN
Forum|alt.badge.img+23

Hey! Some weeks ago I built a super simple, yet very very powerful automation, for helping a friend parse invoices, and thought it would be helpful for others to post a basic guide (and open the discussion for improvements) here.

 



Proof of Concept:

  1. By uploading an Invoice to Airtable.

  2. An automation would use AI to extract, Invoice Line Item Descriptions, Quantity and Totals.

  3. Then the automation would create the corresponding records with precise information.

  4. Therefore my friend can now get all insights from the invoices his company receives (how much they’re spending, what they’re spending on, and a clear picture of overall finances).

 

Needs:

  1. Airtable paid plan for scripting and AI features.
     

Areas of improvement:

  1. Prompt for AI, as well as Json output would need some iterations to allow handling multiple different invoice formats.
  2. Prompt for AI should include an example Json.
  3. Many others for sure
     

High-level setup:

  1. Invoices Table
  2. Invoice Line Items table

Automation:
 

  1. Triggers when attachment field in Invoices table is not empty
  2. AI block prompt: “ {Attachment} contains an invoice. Within it you'll find invoice line items. Each line item will have (i) Description; (ii) Quantity; and (iii) Amount. Please output a json with list of each invoice and each value. Do not output any thing different to the json
  3. Script: 
    // Retrieve the configuration inputs from the automation
    let config = input.config();
    let invoiceJson = config.invoiceJson;
    let recordID = config.recordID;
    
    // If invoiceJson is passed as a string, parse it into an object
    if (typeof invoiceJson === "string") {
      try {
        invoiceJson = JSON.parse(invoiceJson);
      } catch (error) {
        throw new Error("Failed to parse invoiceJson: " + error.message);
      }
    }
    
    // If invoiceJson is not an array, check if it's an object with an "invoices" key that is an array
    if (!Array.isArray(invoiceJson)) {
      if (invoiceJson.invoices && Array.isArray(invoiceJson.invoices)) {
        invoiceJson = invoiceJson.invoices;
      } else {
        throw new Error("invoiceJson is not an array or does not contain an 'invoices' array.");
      }
    }
    
    // Build an array of line item objects from the invoiceJson data
    let lineItems = invoiceJson.map(item => ({
      "Description": item.description,
      "Quantity": item.quantity,
      "Amount": item.amount,
      // If you need to pass the invoice record ID along, include it as needed.
      "Invoice": recordID
    }));
    
    // Output the list of line items so subsequent steps can access it
    output.set("lineItems", lineItems);
    1. Repeating group with Create Record block, for creating records in Line Items table, mapping values from each line item output of the script.

Video going through it.

What other use cases could this apply to? How could this be further improved? Do you have similar workflows in place?

Happy to discuss the above!

Mike, Consultant @ Automatic Nation

0 replies

Be the first to reply!

Reply