Skip to main content

Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation

Workflow preview

Workflow preview
100%
Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

PDF Invoice Extractor (AI) End to end pipeline: Watch Drive ➜ Download PDF ➜ OCR text ➜ AI normalize to JSON ➜ Upsert Buyer (Account) ➜ Create Opportunity ➜ Map Products ➜ Create OLI via Composite ...

Best for

  • Invoice Processing automation workflows
  • AI Summarization automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.stickynote, n8n-nodes-base.extractfromfile, n8n-nodes-base.googledrivetrigger, @n8n/n8n-nodes-langchain.openai, n8n-nodes-base.googledrive, n8n-nodes-base.microsoftonedrive, n8n-nodes-base.salesforce, n8n-nodes-base.code

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Le Nguyen.

Original n8n.io source

1.1 Workflow description

Title
Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation
Workflow name
Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation

PDF Invoice Extractor (AI)

End-to-end pipeline: Watch Drive ➜ Download PDF ➜ OCR text ➜ AI normalize to JSON ➜ Upsert Buyer (Account) ➜ Create Opportunity ➜ Map Products ➜ Create OLI via Composite API ➜ Archive to OneDrive.


Node by node (what it does & key setup)

1) Google Drive Trigger

  • Purpose: Fire when a new file appears in a specific Google Drive folder.
  • Key settings:
    • Event: fileCreated
    • Folder ID: google drive folder id
    • Polling: everyMinute
    • Creds: googleDriveOAuth2Api
  • Output: Metadata { id, name, ... } for the new file.

2) Download File From Google

  • Purpose: Get the file binary for processing and archiving.
  • Key settings:
    • Operation: download
    • File ID: ={{ $json.id }}
    • Creds: googleDriveOAuth2Api
  • Output: Binary (default key: data) and original metadata.

3) Extract from File

  • Purpose: Extract text from PDF (OCR as needed) for AI parsing.
  • Key settings:
    • Operation: pdf
    • OCR: enable for scanned PDFs (in options)
  • Output: JSON with OCR text at {{ $json.text }}.

4) Message a model (AI JSON Extractor)

  • Purpose: Convert OCR text into strict normalized JSON array (invoice schema).
  • Key settings:
    • Node: @n8n/n8n-nodes-langchain.openAi
    • Model: gpt-4.1 (or gpt-4.1-mini)
    • Message role: system (the strict prompt; references {{ $json.text }})
    • jsonOutput: true
    • Creds: openAiApi
  • Output (per item): $.message.content → the parsed JSON (ensure it’s an array).

5) Create or update an account (Salesforce)

  • Purpose: Upsert Buyer as Account using an external ID.
  • Key settings:
    • Resource: account
    • Operation: upsert
    • External Id Field: tax_id__c
    • External Id Value: ={{ $json.message.content.buyer.tax_id }}
    • Name: ={{ $json.message.content.buyer.name }}
    • Creds: salesforceOAuth2Api
  • Output: Account record (captures Id) for downstream Opportunity.

6) Create an opportunity (Salesforce)

  • Purpose: Create Opportunity linked to the Buyer (Account).
  • Key settings:
    • Resource: opportunity
    • Name: ={{ $('Message a model').item.json.message.content.invoice.code }}
    • Close Date: ={{ $('Message a model').item.json.message.content.invoice.issue_date }}
    • Stage: Closed Won
    • Amount: ={{ $('Message a model').item.json.message.content.summary.grand_total }}
    • AccountId: ={{ $json.id }} (from Upsert Account output)
    • Creds: salesforceOAuth2Api
  • Output: Opportunity Id for OLI creation.

7) Build SOQL (Code / JS)

  • Purpose: Collect unique product codes from AI JSON and build a SOQL query for PricebookEntry by Pricebook2Id.
  • Key settings:
    • pricebook2Id (hardcoded in script): e.g., 01sxxxxxxxxxxxxxxx
    • Source lines: $('Message a model').first().json.message.content.products
  • Output: { soql, codes }

8) Query PricebookEntries (Salesforce)

  • Purpose: Fetch PricebookEntry.Id for each Product2.ProductCode.
  • Key settings:
    • Resource: search
    • Query: ={{ $json.soql }}
    • Creds: salesforceOAuth2Api
  • Output: Items with Id, Product2.ProductCode (used for mapping).

9) Code in JavaScript (Build OLI payloads)

  • Purpose: Join lines with PBE results and Opportunity Id ➜ build OpportunityLineItem payloads.
  • Inputs:
    • OpportunityId: ={{ $('Create an opportunity').first().json.id }}
    • Lines: ={{ $('Message a model').first().json.message.content.products }}
    • PBE rows: from previous node items
  • Output: { body: { allOrNone:false, records:[{ OpportunityLineItem... }] } }
  • Notes:
    • Converts discount_total ➜ per-unit if needed (currently commented for standard pricing).
    • Throws on missing PBE mapping or empty lines.

10) Create Opportunity Line Items (HTTP Request)

  • Purpose: Bulk create OLIs via Salesforce Composite API.
  • Key settings:
    • Method: POST
    • URL: https://<your-instance>.my.salesforce.com/services/data/v65.0/composite/sobjects
    • Auth: salesforceOAuth2Api (predefined credential)
    • Body (JSON): ={{ $json.body }}
  • Output: Composite API results (per-record statuses).

11) Update File to One Drive

  • Purpose: Archive the original PDF in OneDrive.
  • Key settings:
    • Operation: upload
    • File Name: ={{ $json.name }}
    • Parent Folder ID: onedrive folder id
    • Binary Data: true (from the Download node)
    • Creds: microsoftOneDriveOAuth2Api
  • Output: Uploaded file metadata.

Data flow (wiring)

  1. Google Drive TriggerDownload File From Google
  2. Download File From Google
    • Extract from File
    • Update File to One Drive
  3. Extract from FileMessage a model
  4. Message a model
    • Create or update an account
  5. Create or update an accountCreate an opportunity
  6. Create an opportunityBuild SOQL
  7. Build SOQLQuery PricebookEntries
  8. Query PricebookEntriesCode in JavaScript
  9. Code in JavaScriptCreate Opportunity Line Items

Quick setup checklist

  • 🔐 Credentials: Connect Google Drive, OneDrive, Salesforce, OpenAI.
  • 📂 IDs:
    • Drive Folder ID (watch)
    • OneDrive Parent Folder ID (archive)
    • Salesforce Pricebook2Id (in the JS SOQL builder)
  • 🧠 AI Prompt: Use the strict system prompt; jsonOutput = true.
  • 🧾 Field mappings:
    • Buyer tax id/name → Account upsert fields
    • Invoice code/date/amount → Opportunity fields
    • Product name must equal your Product2.ProductCode in SF.
  • Test: Drop a sample PDF → verify:
    • AI returns array JSON only
    • Account/Opportunity created
    • OLI records created
    • PDF archived to OneDrive

Notes & best practices

  • If PDFs are scans, enable OCR in Extract from File.
  • If AI returns non-JSON, keep “Return only a JSON array” as the last line of the prompt and keep jsonOutput enabled.
  • Consider adding validation on parsing.warnings to gate Salesforce writes.
  • For discounts/taxes in OLI:
    • Standard OLI fields don’t support per-line discount amounts directly; model them in UnitPrice or custom fields.
  • Replace the Composite API URL with your org’s domain or use the Salesforce node’s Bulk Upsert for simplicity.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 2 - Extract from File

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1

Block 3 - Google Drive Trigger

Type / Role
n8n-nodes-base.googleDriveTrigger - googleDriveTrigger
Config choices
Version 1

Block 4 - Message a model

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 1.8

Block 5 - Download File From Google

Type / Role
n8n-nodes-base.googleDrive - googleDrive
Config choices
Version 3

Block 6 - Update File to One Drive

Type / Role
n8n-nodes-base.microsoftOneDrive - microsoftOneDrive
Config choices
Version 1

Block 7 - Create an opportunity

Type / Role
n8n-nodes-base.salesforce - salesforce
Config choices
Version 1

Block 8 - Create or update an account

Type / Role
n8n-nodes-base.salesforce - salesforce
Config choices
Version 1

Block 9 - Build SOQL

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 10 - Query PricebookEntries

Type / Role
n8n-nodes-base.salesforce - salesforce
Config choices
Version 1

Block 11 - Code in JavaScript

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 12 - Create Opportunity Line Items

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.2

Block 13 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 14 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 15 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 16 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 17 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 18 - Sticky Note6

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Sticky Note7

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 20 - Sticky Note8

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note9

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sticky Note10

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 23 - Sticky Note11

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

3. Summary Table

Workflow Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation
Complexity advanced
Nodes 23
Categories Invoice Processing, AI Summarization
Author Le Nguyen
Published 22 Oct 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/10029/10029.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Automate invoice processing with OCR, GPT-4 & Salesforce opportunity creation do?

PDF Invoice Extractor (AI) End to end pipeline: Watch Drive ➜ Download PDF ➜ OCR text ➜ AI normalize to JSON ➜ Upsert Buyer (Account) ➜ Create Opportunity ➜ Map Products ➜ Create OLI via Composite ...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Invoice Processing, AI Summarization use case.