Scriban

Scriban is a scripting language that can be used to generate values calculated within Ternair. With Scriban, you can, for example, derive an initial from a first name, set a dynamic system date, or calculate the number of days until a birthday.

Scriban scripts are used within the Snapshot block entered via Mapping > + > Calculated value. The output of the script is then included as a column in the snapshot.

General Procedure
The scripts in this document are reusable examples. Adapt them to your own situation as follows:
  • Replace the text between square brackets (for example, [FIELD_NAME] or [DATE_OF_BIRTH]) with the exact name of the source field from the dataset.

  • Enter the entire script (including the opening and closing brackets {{ and }}) into the "Calculated Value" input field.

Notice: Field names are case-sensitive. A misspelled field name will result in an empty or unexpected result.

Determining the initial letter from a first name

Application: Capitalize the first letter of a first name, followed by a period (for example, Jan → J.). This is useful for a formal salutation in emails.

{{ firstName = [FIRST NAME]

 

  if vName | string.whitespace

  else

    (vName | string.slice 0 1 | string.upcase) | string.append "."

  end

}}

 
Explanation: Replace [FIRST_NAME] with the column name containing the first name. The script checks whether the field contains a value; if so, it takes the first letter, capitalizes it, and adds a period. If the field is empty, the initial is also left blank.

Set the system date

Application: Record when a process is running or a change occurs—for example, for logging purposes.

Option A – at the secondary level:

{{

    date.now | date.to_string '%Y-%m-%d %H:%M:%S'

}}

 
Option B – at the millisecond level:

{{

    date.now | date.to_string '%Y-%m-%d %H:%M:%S:%L'

}}

 
Explanation: Both scripts return the current system date and time. The millisecond version adds :%L for extra precision and is useful when multiple changes within the same second need to be uniquely identifiable.

Number of days until a birthday

Application: Calculate how many days are left until a contact's next birthday. This is useful for triggering campaigns for contacts whose birthdays fall within a specific time frame.

{{ vDate = [DATE OF BIRTH]

 

  if (vDate != NULL)

 

    bday = date.parse vDate

    now = date.now

 

    bday_this_year = date.parse (date.now.year) + '-' + (bday.month) + '-' + (bday.day)

    bday_next_year = date.parse (date.now.year + 1) + '-' + (bday.month) + '-' + (bday.day)

    next_bday = bday_this_year > now ? bday_this_year : bday_next_year

 

    days_until_next_bday = math.floor((next_bday - now).total_days)

    days_until_next_bday

 

  else

    NULL

  end

}}

 
Explanation: Replace [BIRTHDATE] with the field containing the birth date. The script first checks whether the birthday is still coming up this year; if not, it calculates for next year. It then returns the number of full days between today and that birthday. If the field is empty, the result is NULL.

Date relative to today (yesterday or tomorrow)

Application: Calculate a date relative to today—for example, to retrieve data from yesterday or to determine an expiration date in the future.

{{

    AddMinusDays = -1 # Adjust this number: negative = past, positive = future

    date.add_days date.now AddMinusDays | date.to_string "%Y-%m-%d"

}}

 
Change the value of AddMinusDays to set the desired date. For example:
  • -1 = yesterday

  • 1 = tomorrow

  • -7 = one week ago

  • 30 = in 30 days


Number of days since a date

Application: Calculate how many days have passed since a specific date. This is commonly used in RFM analyses, for example, to determine how many days ago a customer last made a purchase.

{{ (date.now - [DATE]).days }}

 
Explanation: Replace [DATE] with the date field to be used in the calculation. The script returns the number of full days between today and that date.

Copyright © 2026 Ternair.