AMPscript RetrieveSalesforceObjects (Next)
Query Salesforce records into a rowset with AMPscript RetrieveSalesforceObjects in Marketing Cloud Next. Syntax, examples, and behaviour proven in a Marketing Cloud Next email, including how it differs from Engagement.
Syntax
RetrieveSalesforceObjects(objectName, fieldsToRetrieve, queryFieldName1, queryFieldOperator1, queryFieldValue1[, queryFieldNameN, queryFieldOperatorN, queryFieldValueN, ...]) → rowset
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
objectName |
string | Yes | Object to read records from, written as its API name (for example Contact) |
fieldsToRetrieve |
string | Yes | Field API names, separated by commas, that become the rowset columns |
queryFieldName1 |
string | Yes | Field the first condition tests |
queryFieldOperator1 |
string | Yes | Comparison operator (=, !=, <, <=, >, >=) |
queryFieldValue1 |
string | number | Yes | Value to filter against |
queryFieldNameN |
string | No | Further filter field |
queryFieldOperatorN |
string | No | Further comparison operator |
queryFieldValueN |
string | number | No | Further filter value |
Example
%%[
SET @rows = RetrieveSalesforceObjects("Contact", "Id", "IsDeleted", "=", "false")
IF RowCount(@rows) > 0 THEN
SET @idLength = Length(Field(Row(@rows, 1), "Id"))
ENDIF
]%%
%%=v(@idLength)=%%
Renders 18: the query reached the org’s Contact records and the first Id has 18 characters.
A filter that matches nothing gives an empty rowset:
%%[
SET @none = RetrieveSalesforceObjects("Contact", "Id", "Id", "=", "000000000000000AAA")
]%%
%%=RowCount(@none)=%%
Renders 0.
Return value
rowset — one row per matching record, read with Row and Field. An unmatched filter returns a rowset with RowCount 0.
Behaviour
Queries live records. RetrieveSalesforceObjects("Contact", "Id", "IsDeleted", "=", "false") returned rows, and Field(Row(@rows, 1), "Id") had a length of 18.
No match is not an error. The filter "Id", "=", "000000000000000AAA" returned a rowset with RowCount 0 and the email rendered normally.
Check for no match with RowCount, not Empty. Empty() on that unmatched result rendered false. Test RowCount(@rows) > 0 instead.
An unknown object name fails. An email querying NotARealObject__x saved and published, but rendering it failed with HTTP 500.
The following was tested on 2026-11-25 on the Contact object, one call per email, with the expected counts taken from a SOQL query of the same org:
Comparison operators work as literal strings. "=", ">", ">=", "<" and "<=" on a number field returned the same records as SOQL; for example RetrieveSalesforceObjects("Contact", "LastName,pi__score__c", "pi__score__c", "=", "82") returned the one matching contact, and Field read its last name.
Several conditions combine with AND. "pi__score__c", ">", "290", "LastName", "!=", "Adelson" returned the one record that meets both, and two = conditions no record meets at once returned RowCount 0.
Dates compare as yyyy-MM-dd strings. "Birthdate", "=", "1965-07-26" returned the one contact born that day.
LIKE matches anywhere in the field. The value is found anywhere, regardless of case, and % is not treated as a wildcard: "LastName", "LIKE", "Ro%" returned 5 records where a SOQL prefix match finds 3, and "LIKE", "Bl" returned 2 records where SOQL finds none.
Write the operator as a literal. An operator built at run time fails: Char(60) in place of "<" saved and published, but the preview failed with HTTP 500, and Concat(Char(60), "=") in place of "<=" was rejected at publish with HTTP 400 (“invalid Handlebars script”).
Show Next test script
<!-- Marketing Cloud Next test script for RetrieveSalesforceObjects. 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 = RetrieveSalesforceObjects("Contact","Id","Email","=","x@example.com") ENDIF]%%gated]</p>
<p>c2 expect=[0] got=[%%[ SET @o1rs = RetrieveSalesforceObjects("Contact","Id","Id","=","000000000000000AAA") ]%%%%=RowCount(@o1rs)=%%]</p>
<p>c3 expect=[false] got=[%%[ SET @o2rs = RetrieveSalesforceObjects("Contact","Id","Id","=","000000000000000AAA") ]%%%%=v(Empty(@o2rs))=%%]</p>
<p>c4 expect=[18] got=[%%[ SET @o3rs = RetrieveSalesforceObjects("Contact","Id","IsDeleted","=","false") IF RowCount(@o3rs) > 0 THEN SET @o3r = Row(@o3rs,1) SET @o3v = Length(Field(@o3r,"Id")) ENDIF ]%%%%=v(@o3v)=%%]</p>
<p>c5 expect=[1|Blau] got=[%%[ SET @r1 = RetrieveSalesforceObjects("Contact","LastName,pi__score__c","pi__score__c","=","82") ]%%%%=RowCount(@r1)=%%|%%=Field(Row(@r1,1),"LastName")=%%]</p>
<p>c6 expect=[2] got=[%%[ SET @r2 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c",">","300") ]%%%%=RowCount(@r2)=%%]</p>
<p>c7 expect=[2] got=[%%[ SET @r3 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c",">=","301") ]%%%%=RowCount(@r3)=%%]</p>
<p>c8 expect=[1|Chopra] got=[%%[ SET @r6 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c",">","290","LastName","!=","Adelson") ]%%%%=RowCount(@r6)=%%|%%=Field(Row(@r6,1),"LastName")=%%]</p>
<p>c9 expect=[0] got=[%%[ SET @r7 = RetrieveSalesforceObjects("Contact","LastName","LastName","=","Blau","pi__score__c","=","94") ]%%%%=RowCount(@r7)=%%]</p>
<p>c10 expect=[5] got=[%%[ SET @r11 = RetrieveSalesforceObjects("Contact","LastName","LastName","LIKE","Ro%") ]%%%%=RowCount(@r11)=%%]</p>
<p>c11 expect=[2] got=[%%[ SET @r12 = RetrieveSalesforceObjects("Contact","LastName","LastName","LIKE","Bl") ]%%%%=RowCount(@r12)=%%]</p>
<p>c12 expect=[2] got=[%%[ SET @r8 = RetrieveSalesforceObjects("Contact","LastName","LastName","LIKE","Bl%") ]%%%%=RowCount(@r8)=%%]</p>
<p>c13 expect=[1] got=[%%[ SET @r9 = RetrieveSalesforceObjects("Contact","LastName","Birthdate","=","1965-07-26") ]%%%%=RowCount(@r9)=%%]</p>
<p>c14 expect=[2] got=[%%[ SET @r13 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c","<","35") ]%%%%=RowCount(@r13)=%%]</p>
<p>c15 expect=[2] got=[%%[ SET @r14 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c","<=","34") ]%%%%=RowCount(@r14)=%%]</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 @oe1rs = RetrieveSalesforceObjects("NotARealObject__x","Id","Id","=","000000000000000AAA") ]%%%%=v(RowCount(@oe1rs))=%%
<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%[ SET @r4 = RetrieveSalesforceObjects("Contact","LastName","pi__score__c",Char(60),"35") ]%%%%=RowCount(@r4)=%%
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Yes, from API 67.0 |
For a query that matches nothing, Engagement’s Empty() on the result renders True; Next renders false. The row count (0), a live read of an 18-character Id, and the failure on an unknown object name are the same on both. Compared 2026-11-25 by rendering the same calls in a Marketing Cloud Next email.
See also
RowCount— the reliable no-match checkRowField— read the returned rowset- Official reference