Airscript Examples

Creating a timed event

Sometimes you need to project time out into the future. When scheduling an event and you want to specify a time, Airscript can help you do that through the date functions.

For example to get a get a time two days from now, we could write the following Airscript:

ADD_TO_DATETIME(
  NOW(),
  2,
  "days"
)

In this example we are taking the current time with NOW() adding 2 as number value and "days" as the unit value.

Conditional display of an element

Imagine you have a form where you only want to display an additional item if a condition is TRUE.

On most elements in the app builder there is a Visible option.

airscriptExample1.png

Sometimes you will want to want to do something more than a TRUE or FALSE. In this expression block you can do a bunch of different things.

For example, lets say you have a field you only want to display for persons named "Bob", you could write the following Airscript:

STRING_COMPARE(name, "Bob") = 0

This is checking to see if the name is Bob. STRING_COMPARE() takes in two strings and returns a number. If the strings are equal the number is 0.

Another example may be if you have a list of books and you want to display a message to the user if there are no books selected. Here would be the Airscript to check

ISNOTEMPTY(
  selectedBooks
)

The function ISNOTEMPTY() takes one parameter and checks to see if it is empty, if so it returns FALSE, Otherwise it returns TRUE.

Here are several other ways you could do the same check:

NOT(ISEMPTY(selectedBooks))
// OR
LENGTH(selectedBooks) < 1

Remember that the check is for Visible. If the expression evaluates to TRUE, the element will be visible. If it evaluates to FALSE, it will be hidden.

Formatting a dollar amount

Another common use case is taking a number amount and formatting it for a user to read. It makes sense to keep the numbers in a number format in App Objects for various math operations. Still reading 10000000000 is harder than reading 10,000,000,000. Other countries even use different separators. Luckily Airscript has a FORMAT_CURRENCY() function. You use it like this:

FORMAT_CURRENCY(
  10000000000,
  "USD"
)

In this case "USD" is the currency string. You will get $10,000,000,000.00 out.

Combining a name for a label

Another common example is combining text variables together. Imagine a case where you have an input with three text inputs, one for first name, middle name, and last name. You want to display them combined at the end.

airscriptExample2.png

In order to get out the full name, you can use escape characters in your string to display each part of the name:

"{{}}"

In Airscript Strings {{ }} are known as escape tokens. Inside the escape tokens you have access to Airscript accessible variables and functions.

There are a couple of other ways to do this. For example:

JOIN(
  [ first_name, middle_name, last_name ],
  " "
)

In this example we create an array with the variables and use the JOIN() function to combine them with a space.

Formatting a Date

Depending on your use case, you might want to display dates differently to your user. Sometimes you want to display a shortened, numerical interpretation, other times you may need to include the full date and time. Airscript provides you tools to configure that, regardless of your needs. You can also combine different date functions together. Here is an example:

FORMAT_DATETIME(
  ADD_TO_DATETIME(
    NOW(),
    2,
    "days"
  ),
  "mm/dd/yyyy"
)

Given today’s date (May 20th, 2020) this would result in:

05/20/2020

For more details on how to use the above expressions, check out our previous Airscript Examples, along with looking at the reference material for the Date Functions.

Conditional Expressions

Conditional expressions in Airscript allow you to select one of two expressions based on the boolean result of the first expression. If the expression evaluates to TRUE the second expression will evaluate, if it’s FALSE the third expression will evaluate.

Let’s look at an example. Below we have an simple example of what a ticket might look like:

{
    "id": "123",
    "description": "Hello World"
}

We can create an expression where we check whether the ticket matches the correct id. If it has the correct id, then it’ll return the description string, otherwise it’ll return the string "Not Found"

IF(
  id = "123",
  description,
  "Not Found"
)

Because the second and third parameters are also expressions, we can nest these IF() expressions as well. We could check to make sure that the description also exists, and have a different response if there is no description present.

IF(
  id = "123",
  IF(
    NOT(
      description = ""
    ),
    description,
    "No Description Found"
  ),
  "Not Found"
)

Complex Filtering and Querying

Airscript also provides a complex filtering and manipulation offering similar to SQL. This provides you the power of a query language, on top of the already powerful Airscript.

Let’s take an example where you have a list of objects that contain a question and selected answer. Here we see a list of questions, with only some of the answers filled in:

listOfQuestions
  = [
    { "prompt": "What is your name?", "answer": "Nathan" },
    { "prompt": "Where do you work?", "answer": "Airkit" },
    { "prompt": "What's your favorite drink?", "answer": "" }
  ]

We can filter that list of questions down to only the questions that have answers, and then we can extract only the answers from those:

FROM question IN listOfQuestions WHERE NOT(question.answer = "") SELECT question.answer

That would give us a filtered result of:

["Nathan", "Airkit"]

Since, we’re still working with Airscript, we can also perform more advanced operations on the filtered data. We could join those answers into one string for use within a label. Our filter expression would change to this:

JOIN(
  FROM
    question
  IN
    listOfQuestions
  WHERE
    NOT(
      question.answer = ""
    )
  SELECT
    "Question: {{question.prompt}} - Answer: {{question.answer}}",
  "
"
)

Which would give us the string:

Question: What is your name? - Answer: Nathan
Question: Where do you work? - Answer: Airkit