Liquid query

From v19 we can now create a list of records and optimise how we render them in chunks or bundle many records in one server call. The query tag allows you to fetch records from Salesforce objects directly within your Liquid templates. This powerful feature enables dynamic content based on real-time data.

For instance, you want to bring all records related to the Product2 object in your org, you can simply use the query tag as follows:

{%- query 'Product2' as records -%}

This example shows you that you have access to a variable containing the results (or recordset) of your query. You can access it by the name you defined in the query after the as keyword.

Iterating through records

Once you have pulled the object using the query tag then you can iterate through each record using a standard for loop:

%- for record in records -%}
  {{ record.name }}
{%- endfor -%}

You can access multiple attributes related to the product records.

It’s important to note that you will render ALL records from your org regardless of the store they are related to.

Supported objects

There’s a limit amount of SF objects you access. Here’s a list of supported fields on v19:

  • Product2
  • Account
  • Pricebook2

Parameters

These parameters will allow you call specific records without pulling a large number of records, they work similarly to liquid global lookups.

Use name if you want to explicitly call a specific product

{%- query 'Product2' records, name: 'Product Name' -%}

You also use sfid

{%- query 'Product2' records, name: 'Product Name' -%}

Additional features

StoreConnect liquid version 1.1.0 and up supports new List which helps you to declare array variables, this feature isn’t necessarily only part of liquid queries but it can be broadly used when it comes to build arrays in liquid.

Previously, there wasn’t a explicit way to declare variables that would contain multiple objects and then make it iterable unless you were applying filters to iterate through the data stored in that variable.

Example:

{%- assign slug2 = 'Product-2' %}

{%- new List slugs = '["product-1"]' %}
{%- assign slugs = slugs | push: slug2 %}

{%- query 'Product2' as records, s_c__slug__c: slugs -%}

{%- for record in records %}
  {{ record.s_c__slug__c }}
{%- endfor %}