Handlebars queryFirst
Handlebars queryFirst: returns the first matching record from a Sales Cloud object, a marketing object or a data graph.
Returns the first matching record from a Sales Cloud object, a marketing object or a data graph.
Syntax
{{queryFirst object type [...options]}}
Returns object.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
object |
string | Yes | The API name of the target object, marketing object, or data graph. |
type |
string | Yes | The data source type: CRM, MO, or DG. |
options |
any | No, repeatable | Named arguments such as fields, where, params, sortBy, id. |
Example
Tested in Marketing Cloud Next emails on 2026-11-25, one call per email, against a test marketing object zzprobecoupons__mo with the key field CouponCode__c:
{{get (queryFirst type="MO" object="zzprobecoupons__mo" CouponCode__c="PROBE0001") "IsClaimed__c"}}rendersfalse, the stored value.- Without the key filter (the call on its own, inside
#with, or insideget): the preview fails with HTTP 500 and no message. - A CRM call inside
{{#if false}}…{{/if}}is accepted when the email is saved.
On the Contact object (type="CRM"), with the expected record taken from a SOQL query of the same org:
{{get (queryFirst type="CRM" object="Contact" fields="LastName" where="pi__score__c = :s" params=(hash s=82)) "LastName"}}renders the last name of the one matching contact.- With
where="pi__score__c > :s" params=(hash s=200),sortBy="pi__score__c DESC"returns the contact with the highest score andsortBy="pi__score__c ASC"the one with the lowest. - When nothing matches (
s=1),[{{get (queryFirst …) "LastName"}}]renders[]and the email renders normally.
This is the Handlebars counterpart of AMPscript Lookup: Lookup("zzprobecoupons__mo","IsClaimed__c","CouponCode__c","PROBE0001") rendered the same false.
Thanks to Nobuyuki Watanabe’s SFMC Tips #327 for the get (queryFirst type="MO" …) "Field" pattern as the replacement for AMPscript Lookup, and SFMC Tips #313 for the hint that every key field of a marketing object belongs in the filter.
Show Next test script
Show calls that fail in Next (one email each)
Differs from official docs
- On a marketing object (
type="MO"), the call needs at least one inline filter, such asKeyField__c="value"; a field other than the key works too. Without one, the preview fails with HTTP 500 and no message, although the reference lists onlyobjectandtypeas required.sortByfails the same way. Details
Marketing Cloud Next
On Sales Cloud objects, where with params and sortBy pick the expected record, and no match renders empty. On a marketing object it works when an inline filter is given (on the key or another field); without one, or with sortBy, the preview fails with HTTP 500. Tested 2026-11-25 by rendering in a Marketing Cloud Next email.
See also
- AMPscript
Lookup— does a similar job in AMPscript, with a different call shape - Official reference