Skip to content

Conversation

bgaidioz
Copy link
Contributor

@bgaidioz bgaidioz commented Apr 1, 2025

It works like that.

SELECT *
FROM salesforce.soql('SELECT Id, Name, Alias, Department, EmployeeNumber FROM User') u;

returns an array of JSONB, which is exposed as a table of one column.

Each item has that shape below. A JSON object with the required fields + some metadata field (attributes) that comes as well with the query results.

{
  "Id": "005Qy0000055xXdIAI",
  "Name": "Integration User",
  "Alias": "integ",
  "Department": null,
  "attributes": {
    "url": "/services/data/v59.0/sobjects/User/005Qy0000055xXdIAI",
    "type": "User"
  },
  "EmployeeNumber": null
}

One can eventually process each row using Postgres' JSONB features:

SELECT u->>'Name' as name, u->>'Alias' as alias
FROM salesforce.soql('SELECT Id, Name, Alias, Department, EmployeeNumber FROM User') u;

How to use Postgres to forge the SOQL string isn't the scope of the changeset, but here's an idea. The code below aggregates forged predicates using AND while skipping NULL items (and if all are NULL, string_add evaluates to NULL). As they're entered, expressions boil down to NULL if parameters are null (concatenation with || and quote_literal or to_char propagate the null value).

-- @type company VARCHAR
-- @type min_date DATE
-- @type max_date DATE
-- @default company NULL
-- @default min_date NULL
-- @default max_date NULL
SELECT CONCAT('SELECT * FROM table', ' WHERE ' || clause, ' ORDER BY ...')
FROM (
    SELECT STRING_AGG(predicate, ' AND ') AS clause
    FROM (
      VALUES
        ('company = ' || quote_literal(:company)),
        ('date >= ' || to_char(:min_date, 'YYYY-MM-DD')),
        ('date <= ' || to_char(:max_date, 'YYYY-MM-DD'))
    ) AS t(predicate)
) whereComputation
params result
min_date=2025-03-01 SELECT * FROM table WHERE date >= 2025-03-01 ORDER BY ...
min_date=2025-03-01&max_date=2025-04-01 SELECT * FROM table WHERE date >= 2025-03-01 AND date <= 2025-04-01 ORDER BY ...
company=Microsoft SELECT * FROM table WHERE company = 'Microsoft' ORDER BY ...
nothing SELECT * FROM table ORDER BY ...

@bgaidioz bgaidioz force-pushed the RD-15450-salesforce-soql-function branch 2 times, most recently from c87a0d3 to 50e2e49 Compare May 12, 2025 09:42
@bgaidioz bgaidioz force-pushed the RD-15450-salesforce-soql-function branch from 50e2e49 to e02362c Compare May 21, 2025 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant