Query Data Extension by Email Without Primary Key

Question
I have a Data Extension in Salesforce Marketing Cloud where the primary key is not the Email field. I need to make an API call to find all records that match a given email address.
While going through the documentation, I found that the REST API allows retrieving data, but it specifically states that filters using $filter
can only be applied on primary keys. This means I cannot filter records directly using the Email field since it is not set as a primary key.
Am I missing something in the documentation, or is there a better way to achieve this? Should I consider changing my Data Extension’s primary key to include the Email address, or is there another solution for searching by non-primary key?
Answer
You are correct that the REST API limits filtering to primary keys, so you cannot directly query by a non-primary key such as Email. However, you do have some alternatives to achieve the result.
CRS Info Solutions offers expert Salesforce online training with real-time projects, certification guidance, interview coaching, and a job-ready approach. Enroll for free demo today!!!
One option is to create a custom API endpoint in Marketing Cloud using a Code Resource with Server-Side JavaScript (SSJS). This allows you to perform a lookup on any field in your Data Extension and return the results in JSON format.
Here’s an example:
<script runat="server">
Platform.Load("core", "1.1.1");
var jsonOut = {};
var recordRows = [];
try {
var email = Request.GetQueryStringParameter("email");
var dataRows = Platform.Function.LookupRows(
"Your DE name",
"Your email field name",
email
);
if (dataRows && dataRows.length > 0) {
for (var i = 0; i < dataRows.length; i++) {
recordRows.push(dataRows[i]);
}
}
// Add attributes to JSON output
jsonOut["emailFilter"] = email;
jsonOut["recordCount"] = recordRows.length;
jsonOut["recordRows"] = recordRows;
Write(Stringify(jsonOut));
} catch (ex) {
Write(Stringify(ex));
}
</script>
Explanation:
This SSJS code creates a JSON-based API endpoint in Marketing Cloud that accepts an email parameter from the query string, looks up matching rows in a specified Data Extension using the LookupRows
function, and then returns the results in JSON format with the email filter, record count, and matching rows.
You would then call this endpoint like so:
GET https://YOUR_RESOURCE_PAGE_URL?email={EmailToFilter}
Content-Type: application/json
This approach allows you to search by Email even when it is not the primary key.
Alternative Approach
If you have flexibility in your Data Extension design, another solution would be to make the Email field a primary key or include it as part of a composite primary key. This way, you can directly use the REST API $filter
parameter without the need for custom code.
Summing Up
In Salesforce Marketing Cloud, you cannot directly filter by a non-primary key field like Email using the REST API, since $filter
only supports primary keys. To work around this, you can build a custom API endpoint with a Code Resource and SSJS, which lets you query a Data Extension by any field and return results in JSON format.
Alternatively, you can redesign your Data Extension to make Email a primary key or part of a composite key, enabling direct REST API queries. Both approaches ensure flexibility, but the custom Code Resource method provides a quick solution without changing your existing DE structure.
Empower Your Career with Salesforce Training in India
Elevate your professional journey with CRS Info Solutions’ top-rated salesforce training, crafted to provide the skills and expertise required to excel in the ever-evolving Salesforce ecosystem. Our industry-focused courses span Salesforce Admin, Developer, and AI modules, blending in-depth theoretical knowledge with hands-on experience through practical, real-world projects. Whether you’re new to Salesforce or a seasoned professional, our well-structured program ensures you master the tools and techniques needed to stand out.
With a strong emphasis on practical application, Salesforce training in India we offer personalized mentorship, detailed study resources, and expert-led certification preparation to fully equip you for success in interviews and beyond. Gain the confidence and skills to thrive in your Salesforce career.
Don’t wait—join our free demo class today and take the first step toward a rewarding future! Enroll now for a free demo!!