AMPscript LookupOrderedRows (Next)
AMPscript LookupOrderedRows in Marketing Cloud Next: an email containing the call saves, but every rendered call on a marketing object failed. Use LookupRows instead.
Syntax
LookupOrderedRows(dataExt, numRows, sortColumn, searchColumn1, searchValue1[, searchColumnN, searchValueN, ...]) → rowset
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
dataExt |
string | Yes | Name or external key of the data extension to read |
numRows |
number | Yes | Maximum rows to return; a value below 1 returns all matches (up to 2,000) |
sortColumn |
string | Yes | Column to sort by, optionally followed by ASC or DESC |
searchColumn1 |
string | Yes | First column to filter on |
searchValue1 |
string | number | Yes | Value the first column must equal |
searchColumnN |
string | No | Further filter columns, each paired with a value |
searchValueN |
string | number | No | Value the corresponding further column must equal |
Example
%%[
SET @rows = LookupOrderedRows("zzprobecoupons__mo", 5, "CouponCode__c DESC", "CouponCode__c", "PROBE0001")
]%%
%%=RowCount(@rows)=%%
The email saves and publishes, but the preview fails with HTTP 500 and no message. The test marketing object zzprobecoupons__mo holds a record with the key PROBE0001, and LookupRows with the same filter returns it.
Return value
rowset — the declared return type. No rendered call has returned a result in Next.
Behaviour
Tested in Marketing Cloud Next emails on 2026-11-25, one call per email, on the test marketing object zzprobecoupons__mo (key field CouponCode__c):
Every rendered call failed. The preview answered HTTP 500 with no message for each form tried: a row count of 0, 1 and 5; the sort column with ASC, DESC, lowercase desc and no direction; a filter on the key field and a filter on another field ("IsClaimed__c", "false"). The failures repeated with longer waits for the preview.
The syntax is accepted at save. An email with LookupOrderedRows("ProbeDE", 1, "K asc", "K", "v") inside an IF 1 == 2 branch saved and published (2026-11-23), and the rest of the email rendered. A call that runs is only caught when the email is rendered.
Use LookupRows instead. It returns the same marketing object rows with the same filters; it does not sort or cap them.
Show Next test script
<!-- Marketing Cloud Next test script for LookupOrderedRows. Paste it into the HTML component of a
Next email and open the preview. Each line prints the result Next gave when the call
was rendered on its own (expect) next to the result of this run (got).
Assembled from single-call test emails (dates on the page); it has not been
run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[gated] got=[%%[IF 1 == 2 THEN SET @r = LookupOrderedRows("ProbeDE",1,"K asc","K","v") ENDIF]%%gated]</p>
Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
would stop the whole preview or block the save. -->
<!-- email 1: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @lo1 = LookupOrderedRows("zzprobecoupons__mo",5,"CouponCode__c DESC","CouponCode__c","PROBE0001") ]%%%%=RowCount(@lo1)=%%|%%=Field(Row(@lo1,1),"CouponCode__c")=%%
<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @lo2 = LookupOrderedRows("zzprobecoupons__mo",0,"CouponCode__c ASC","CouponCode__c","PROBE0001") ]%%%%=RowCount(@lo2)=%%
<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @lo3 = LookupOrderedRows("zzprobecoupons__mo",1,"CouponCode__c DESC","IsClaimed__c","false") ]%%%%=RowCount(@lo3)=%%
<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @lo4 = LookupOrderedRows("zzprobecoupons__mo",1,"CouponCode__c","CouponCode__c","PROBE0001") ]%%%%=RowCount(@lo4)=%%
<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @lo5 = LookupOrderedRows("zzprobecoupons__mo",1,"IsClaimed__c desc","CouponCode__c","PROBE0001") ]%%%%=RowCount(@lo5)=%%
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Not listed for Next in the official AMPscript reference. The language catalog lists it from API 68.0; an email containing it saves, but every rendered call failed (tested 2026-11-25) |
Engagement returns the matching data extension rows sorted by the given column and capped at numRows. In Next, every call tested on a marketing object failed the email preview with HTTP 500, although the email saved and published. Compared 2026-11-25 by rendering the same calls in a Marketing Cloud Next email.
See also
LookupRows— unordered matching rowsLookupRowCountRow- Official reference