Automate Invoice Processing with OCR, GPT-4 & Salesforce Opportunity Creation
    100%
    Automation
    0(0 reviews)
    128 downloads

    Automate Invoice Processing with OCR, GPT-4 & Salesforce Opportunity Creation

    NM

    Curated by n8n Markets team

    Last updated: March 8, 2026

    # 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 Trigger → Download File From Google 2. Download File From Google - → Extract from File - → Update File to One Drive 3. Extract from File → Message a model 4. Message a model - → Create or update an account 5. Create or update an account → Create an opportunity 6. Create an opportunity → Build SOQL 7. Build SOQL → Query PricebookEntries 8. Query PricebookEntries → Code in JavaScript 9. Code in JavaScript → Create Opportunity Line Items --- ## Quick setup checklist - 🔐 Crede

    What's Included

    • Complete n8n workflow template with 0 nodes. Ready to import and customize.

    Requirements

    n8n instance (self-hosted or cloud). credentials.

    How to Use

    1. Download the workflow JSON 2. Import into your n8n instance 3. Configure credentials for 4. Activate and enjoy automation

    How to import this template into n8n

    1. Download the workflow JSON file from this page.
    2. In n8n, open Workflows, create a new workflow and choose Import from File.
    3. Open each node that shows a warning and connect your own credentials.
    4. Run the workflow once manually to check the output before you activate it.
    Included in Pro
    • Importable n8n workflow JSON file
    • Download with Builder Pro or Agency Unlimited
    • Built for n8n v1.0++
    • Setup time: 15-30 min

    Included in Builder Pro — $19/mo, 4,177 premium workflows

    Before you activate

    Templates are reviewed before they are listed. Test with your own credentials and data before running it in production.

    Updated Mar 2026
    v1.0
    SSL
    Secure
    GDPR

    100% Secure Checkout

    Refund requests within 7 days of a new subscription

    Dependencies & Requirements

    Salesforce
    CRM
    Google Drive
    Storage

    ⚠️ 2 services require credentials. Make sure you have accounts set up before importing.

    Reviews

    0

    No reviews yet

    See all 0 reviews ↓
    —

    0 reviews

    5★
    0
    4★
    0
    3★
    0
    2★
    0
    1★
    0

    No reviews yet.

    Questions & Answers

    0

    Log in to ask a question.

    No questions yet. Be the first to ask!

    People Also Bought