Handlebars query: returns many records from a Sales Cloud object or a marketing object.
Next
Verification in progress
Differs from official docs
Test scripts included
Returns many records from a Sales Cloud object or a marketing object.
Syntax
{{query...options}}
Returns array.
Parameters
Name
Type
Required
Description
options
any
Yes, repeatable
Named arguments only: object (API name of the target object) and type (CRM or MO) are required; fields, where, params, limit, sortBy and id are optional.
Example
Tested in Marketing Cloud Next emails on 2026-11-25, one call per email.
On a test marketing object zzprobecoupons__mo with the key field CouponCode__c:
The same call with fields="CouponCode__c" and no key filter: the preview fails with HTTP 500 and no message.
where with params does not replace the inline key filter: where="CouponCode__c = :c" (also with LIKE) fails the preview with HTTP 500, and so does where next to an inline filter, which the reference rules out anyway.
On the Contact object (type="CRM"), with the expected rows taken from a SOQL query of the same org:
{{#each (query type="CRM" object="Contact" fields="LastName,pi__score__c" where="pi__score__c = :s" params=(hash s=82))}}{{LastName}};{{/each}} renders the one matching contact. A string param (s="82") on the number field matches too.
= compares strings without regard to case: where="LastName = :n" with n="blau" finds the contact Blau.
!=, <, <=, > and >= on a number field return the same records as SOQL. On a date field, =, < and > work with the date passed as a "yyyy-MM-dd" string, for example where="Birthdate = :d" params=(hash d="1965-07-26").
AND, OR and parentheses combine conditions: where="LastName LIKE :n AND (pi__score__c < :lo OR pi__score__c > :hi)" returns the expected records.
sortBy="pi__score__c ASC" and "pi__score__c DESC" order the rows, and limit=2 keeps the first two rows after sorting.
LIKE matches the value anywhere in the field, ignoring case, and does not treat % as a wildcard: n="Ro%" returns Brown, Frost, Rogers, Rose and Rossi (SOQL returns only the last three); n="Bl" returns Blay and Blau (SOQL returns none).
Only the fields listed in fields are filled: with fields="LastName", {{FirstName}} and the filter field render empty. Without fields, the matching record comes back but every field renders empty.
A value written straight into where, without a :placeholder, fails the preview with HTTP 500: where="LastName = 'Blau'", where="pi__score__c = 82", and a mix of a literal with a placeholder.
{{#if false}}{{query object="Contact" type="CRM" fields="Id" limit=1}}{{/if}} is accepted when the email is saved.
Use named parameters only; the helper is an inline helper, so loop over its result with each.
Thanks to Nobuyuki Watanabe’s SFMC Tips #327 for the type="MO" call with an inline key filter, and SFMC Tips #313 for the hint that every key field of a marketing object belongs in the filter.
Show Next test script
<!-- Marketing Cloud Next test script for the query helper. 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=[{{#iffalse}}{{queryobject="Contact"type="CRM"fields="Id"limit=1}}{{/if}}gated]</p><p>c2 expect=[PROBE0001;] got=[{{#each(querytype="MO"object="zzprobecoupons__mo"CouponCode__c="PROBE0001")}}{{CouponCode__c}};{{/each}}]</p><p>c3 expect=[Blau;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c = :s"params=(hashs=82))}}{{LastName}};{{/each}}]</p><p>c4 expect=[Blau;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName = :n"params=(hashn="Blau"))}}{{LastName}};{{/each}}]</p><p>c5 expect=[Blau;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName = :n"params=(hashn="blau"))}}{{LastName}};{{/each}}]</p><p>c6 expect=[Blau;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c = :s"params=(hashs="82"))}}{{LastName}};{{/each}}]</p><p>c7 expect=[Chopra;Adelson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c > :s"params=(hashs=300)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c8 expect=[Chopra;Adelson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c >= :s"params=(hashs=301)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c9 expect=[Phillips;Partner;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c < :s"params=(hashs=35)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c10 expect=[Phillips;Partner;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c <= :s"params=(hashs=34)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c11 expect=[Chopra;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName != :n AND pi__score__c > :s"params=(hashn="Adelson"s=290))}}{{LastName}};{{/each}}]</p><p>c12 expect=[Brown;Frost;Rogers;Rose;Rossi;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n"params=(hashn="Ro%")sortBy="LastName ASC")}}{{LastName}};{{/each}}]</p><p>c13 expect=[Blay;Blau;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n"params=(hashn="Bl"))}}{{LastName}};{{/each}}]</p><p>c14 expect=[Brennaman;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n"params=(hashn="%ennam%"))}}{{LastName}};{{/each}}]</p><p>c15 expect=[Bowden;Brown;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n AND pi__score__c > :s"params=(hashn="B%"s=150)sortBy="LastName ASC")}}{{LastName}};{{/each}}]</p><p>c16 expect=[Phillips;Adelson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c = :a OR pi__score__c = :b"params=(hasha=27b=345)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c17 expect=[Brennaman;Brown;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n AND (pi__score__c < :lo OR pi__score__c > :hi)"params=(hashn="B%"lo=50hi=200)sortBy="LastName ASC")}}{{LastName}};{{/each}}]</p><p>c18 expect=[Brennaman;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="Birthdate = :d"params=(hashd="1965-07-26"))}}{{LastName}};{{/each}}]</p><p>c19 expect=[Brown;Green;Jones;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="Birthdate > :d AND pi__score__c > :s"params=(hashd="1988-01-01"s=0)sortBy="LastName ASC")}}{{LastName}};{{/each}}]</p><p>c20 expect=[Bergman;Bowden;Distributor;Lambert;O&#39;Brien Jr;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName LIKE :n AND Birthdate < :d"params=(hashn="B%"d="1963-01-01")sortBy="LastName ASC")}}{{LastName}};{{/each}}]</p><p>c21 expect=[Jones;Pearson;Jeffries;Brown;Templey;Chopra;Adelson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c > :s"params=(hashs=200)sortBy="pi__score__c ASC")}}{{LastName}};{{/each}}]</p><p>c22 expect=[Adelson;Chopra;Templey;Brown;Jeffries;Pearson;Jones;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c > :s"params=(hashs=200)sortBy="pi__score__c DESC")}}{{LastName}};{{/each}}]</p><p>c23 expect=[Jones;Pearson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c > :s"params=(hashs=200)sortBy="pi__score__c ASC"limit=2)}}{{LastName}};{{/each}}]</p><p>c24 expect=[Blau..;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName"where="pi__score__c = :s"params=(hashs=82))}}{{LastName}}.{{FirstName}}.{{pi__score__c}};{{/each}}]</p><p>c25 expect=[..;] got=[{{#each(querytype="CRM"object="Contact"where="pi__score__c = :s"params=(hashs=82))}}{{LastName}}.{{FirstName}}.{{pi__score__c}};{{/each}}]</p><p>c26 expect=[5] got=[{{#each(querytype="MO"object="zzprobecoupons__mo"fields="CouponCode__c"where="CouponCode__c = :c"params=(hashc="PROBE0001"))}}{{CouponCode__c}};{{/each}}]</p><p>c27 expect=[7.0|seven|70.0;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"CouponNo__c=7)}}{{CouponNo__c}}|{{Label__c}}|{{Points__c}};{{/each}}]</p><p>c28 expect=[7.0|seven;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"CouponNo__c="7")}}{{CouponNo__c}}|{{Label__c}};{{/each}}]</p><p>c29 expect=[] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"CouponNo__c=99)}}{{Label__c}};{{/each}}]</p><p>c30 expect=[seventy;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Points__c=7)}}{{Label__c}};{{/each}}]</p><p>c31 expect=[seven;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"CouponNo__c=7Points__c=70)}}{{Label__c}};{{/each}}]</p><p>c32 expect=[12.0;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Label__c="twelve")}}{{CouponNo__c}};{{/each}}]</p><p>c33 expect=[PROBE0001;] got=[{{#each(querytype="MO"object="zzprobecoupons__mo"IsClaimed__c=false)}}{{CouponCode__c}};{{/each}}]</p><p>c34 expect=[8.0;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Points__c=80.0)}}{{CouponNo__c}};{{/each}}]</p><p>c35 expect=[] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Label__c="none")}}{{CouponNo__c}};{{/each}}]</p><p>c36 expect=[seven;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"CouponNo__c=7fields="Label__c")}}{{Label__c}};{{/each}}]</p><p>c37 expect=[group;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Label__c="group"limit=1)}}{{Label__c}};{{/each}}]</p><p>c38 expect=[8.0;] got=[{{#each(querytype="MO"object="zzprobenumkey__mo"Label__c="group")}}{{CouponNo__c}};{{/each}}]</p><p>c39 expect=[Brown;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName"where="LastName = :n AND MailingCity != :c"params=(hashn="Brown"c=null))}}{{LastName}};{{/each}}]</p><p>c40 expect=[Adelson;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName"where="pi__score__c > :s"params=(hashs=300)sortBy="LastName ASC"limit="1")}}{{LastName}};{{/each}}]</p><p>c41 expect=[Brown|;] got=[{{#each(querytype="CRM"object="Contact"fields="LastName,Account.Name"where="LastName = :n"params=(hashn="Brown"))}}{{LastName}}|{{Account.Name}};{{/each}}]</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) -->{{#each(queryobject="zzprobecoupons__mo"type="MO"fields="CouponCode__c")}}{{CouponCode__c}};{{/each}}<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName"where="LastName = 'Blau'")}}{{LastName}};{{/each}}<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="pi__score__c = 82")}}{{LastName}};{{/each}}<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName,pi__score__c"where="LastName = 'Blau' AND pi__score__c = :s"params=(hashs=82))}}{{LastName}};{{/each}}<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="MO"object="zzprobecoupons__mo"CouponCode__c="PROBE0001"fields="CouponCode__c"where="IsClaimed__c = :f"params=(hashf=false))}}{{CouponCode__c}};{{/each}}<!-- email 6: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="MO"object="zzprobecoupons__mo"fields="CouponCode__c"where="CouponCode__c LIKE :c"params=(hashc="PROBE%"))}}{{CouponCode__c}};{{/each}}<!-- email 7: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="MO"object="zzprobenumkey__mo"Label__c="group"sortBy="Points__c DESC")}}{{CouponNo__c}};{{/each}}<!-- email 8: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName"where="pi__score__c > :a AND LastName <> :n"params=(hasha=300n="Chopra")sortBy="LastName ASC")}}{{LastName}};{{/each}}<!-- email 9: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="FirstName,LastName"where="FirstName = :f"params=(hashf="Sarah")sortBy="FirstName ASC, LastName DESC")}}{{LastName}};{{/each}}<!-- email 10: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName"where="LastName IN (:a, :b)"params=(hasha="Brown"b="Blau")sortBy="LastName ASC")}}{{LastName}};{{/each}}<!-- email 11: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName"where="pi__score__c > :s AND LastName NOT IN (:a, :b)"params=(hashs=300a="Chopra"b="Nobody")sortBy="LastName ASC")}}{{LastName}};{{/each}}<!-- email 12: expect that the email saves, but the preview fails (HTTP 500) -->{{#each(querytype="CRM"object="Contact"fields="LastName"where="pi__score__c > :s AND NOT (LastName = :n)"params=(hashs=300n="Chopra"))}}{{LastName}};{{/each}}
Differs from official docs
On a marketing object (type="MO"), the call needs at least one inline filter, such as KeyField__c="value"; it may name any field, and several combine with AND. Without one, the preview fails with HTTP 500 and no message, although the reference lists only object and type as required. A where filter with params fails the same way, and so does sortBy; limit works. In a test where the filter matched two records, only one was returned. Details
The reference’s grouping example writes values straight into where. In Next (tested 2026-11-25), any value written into where instead of a :placeholder failed the preview with HTTP 500; pass every value through params. Details
The reference lists LIKE as a where operator without saying how it matches. In Next (tested 2026-11-25), LIKE matched the value anywhere in the field and did not treat % as a wildcard, so "Ro%" also matched Brown. Details
The reference marks fields as optional and shows calls without it. In Next (tested 2026-11-25), a call without fields returned the matching record with every field empty. Details
Marketing Cloud Next
Differs from the documentation
On Sales Cloud objects, where with params filters, sorts and limits as documented, but a value written straight into where fails the preview with HTTP 500, LIKE matches anywhere in the field without treating % as a wildcard, and a call without fields returns empty fields. On a marketing object only inline filters work (on any field, several joined with AND, limit allowed, sortBy fails); without one, or with where, the preview fails with HTTP 500. Tested 2026-11-25 by rendering in a Marketing Cloud Next email.