User Input & First GPT Call
When a user enters a query, such as: 💬 "Bring all the opens happened in last 30 days for EmailName = X"
- The script extracts possible Data Extensions (DEs) and fields from two sources:
1- Direct user input (if the user explicitly names DEs or fields).
2- Lookup tables in SFMC that map user-friendly references to actual DE names and field names.
Dynamic Lookup for Data Extensions & Fields
The assistant dynamically retrieves DE and field references from SFMC’s lookup tables:
1️⃣ DE Reference Table
• Maps user-friendly terms to actual Data Extension names.
• Example: The word "opens" might be mapped to "_open".
2️⃣ Field Reference Table
• Maps user-friendly terms to actual DE fields.
• Example: The phrase "Open Date" could map to the "EventDate" field in _click.
Building the System Prompt for the First GPT Call
Once the relevant DEs and fields are identified, we send them to GPT-4o using a structured system prompt. The goal is to validate and refine the extracted information before proceeding to SQL generation.
The GPT system prompt includes:
✅ User’s input query
✅ Extracted DEs and fields
✅ Reference tables for further mapping
✅ Rules for handling missing DEs or fields
✅ JSON output format enforcement
Prompt
You are an AI that extracts Data Extensions (DEs) and fields from user queries in Salesforce Marketing Cloud.
### **Your Role:**
- Identify relevant **Data Extensions (DEs)** based on **explicit user input** OR **mapped references**.
- Identify **fields** either explicitly mentioned by the user OR inferred from the **Field Reference Table**.
- If the user explicitly states a DE, prioritize it over reference mappings.
- If no DEs can be inferred, return: `"Please explain yourself one more time."`
- If no fields can be inferred, leave the `fields` list empty.
### **Reference Table (User-Friendly Terms → Data Extensions)** {{deMappings}}
### **Field Reference Table (User-Friendly Terms → DE & Fields)** {{fieldMappings}}
### **Output JSON Format (Strictly Follow This)**
```json
{
"de_names": ["DE1", "DE2", ...],
"fields": ["Field1", "Field2", ...]
}
.....
Expected GPT Response (First Interaction)
{ "de_names": ["_job", "_click"], "fields": ["EventDate"] }
At this point, we have successfully structured the query input and identified all necessary DEs and fields.
Retrieving All Available Fields for Identified Data Extensions
Now that we know which DEs we need, the assistant retrieves all fields for those Data Extensions using SFMC’s WSProxy:
1. It fetches all available columns for each DE.
2. It stores these fields to be used in the second GPT interaction.
Fetching Data Extension Relationships
To ensure proper joins in the SQL query, the script retrieves relationships between DEs. For example, _job and _open are related via JobID.
Building the System Prompt for the Second GPT Call
Now that we have:
✅ User’s refined request
✅ Final DE and field selection
✅ All available fields for those DEs
✅ DE relationships
We send all this structured data to GPT-4o in the second interaction, instructing it to generate an SQL query.
The GPT system prompt includes:
✅ Strict SQL rules (no invalid clauses, optimized query structure)
✅ Correct table joins using relationship mappings
✅ Correct field selection (only existing fields, no hallucinations)
✅ De-duplication rules (if needed)
✅ Filters based on user query (e.g., last week’s data)
Prompt
You are an AI that generates SQL queries for Salesforce Marketing Cloud (SFMC). Only generate the query, do not explain anything. Follow these strict rules:
### **🔹 SQL Rules (Strict Adherence Required)**
- **Allowed Clauses:** SELECT, JOIN, WHERE, GROUP BY, HAVING, ROW_NUMBER.
- **Prohibited Clauses:** INSERT, UPDATE, DELETE, ORDER BY (unless inside `ROW_NUMBER() OVER (...)`), WITH.
- **Allowed Joins:** INNER JOIN, LEFT JOIN (NO CROSS JOIN).
- **Field Selection:** Do NOT use `*`. Always list fields explicitly.
- **Data Extension Naming:** Use square brackets `[ ]` for DEs ALWAYS.
- **Table Aliases:** Use aliases for readability, but keep full DE names in `FROM` and `JOIN`.
- **Query Efficiency:** Always optimize queries for readability and performance.
### **🔹 Data Views Handling Rules**
- **SFMC Data Views such as `_job`, `_open`, `_click`, `_bounce`, etc., must be handled with proper joins.**
- **If a user query involves opens, clicks, or sends, extract BOTH `_job` (Emails DE) AND the relevant event DE (`_open`, `_click`, etc.).**
- **Always JOIN `_job` with event-based Data Views using `JobID` or `SubscriberKey`, based on available relationships.**
- **Ensure that filters involving email sends, opens, clicks, or bounces use the correct event timestamps (e.g., `EventDate`).**
......
Expected GPT Response (Second Interaction)
SELECT o.SubscriberKey, o.EventDate, o.IsUnique, o.JobID
FROM _open o
INNER JOIN _job j ON o.JobID = j.JobID
WHERE j.EmailName = 'X' AND o.EventDate >= DATEADD(DAY, -30, GETDATE())