---
title: "Validation and Filter Logic using JsonQuery"
canonical: "https://help.fieldbuddy.com/space/FBDOCS/3113911148/Validation%20and%20Filter%20Logic%20using%20JsonQuery"
format: markdown
---
> Macro (toc)

## Overview

Validation Rules and Filter Logic share the same logic engine ( JsonQuery ). This engine checks a formula against a data set, and return value true or false

Filter-logic allows to show/hide certain sections in [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/4350672933](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/4350672933) conditionally, while Validations allow to prevent certain state to be achieved - for example putting work order into particular status or closing id, as well as displaying section on creation/editing of Work Order Line Items or Timesheet line items.

It is described by a formula, and other properties (like data source for a formula or validation error message for example)

## **Formula**

### Definition

1. **Format: **The basic format of the formula looks like: `Table[filters]`
  1. **Examples: **
    1. `FIELDBUDDY__Work_Order__c[*]`    -> Returns an array with all the work orders
    2. `FIELDBUDDY__Work_Order__c[Id=a111l000000vRGlAAM]`   -> Returns only one work work order with the ID `a111l000000vRGlAAM`
  2. **Formula Operators:**
    1. Inside the brackets we can create complex conditions using logic operators:
      - `*` : Returns an array with all the possible results. Without it it will return only the first result
      - `&` : AND condition. Ex: `"FIELDBUDDY__Work_Order_Line_Item__c[FIELDBUDDY__Work_Order__c=${CURRENT_WORK_ORDER}&Status__c=Open]"` -> Returns only one Work Order Line Item related to the current work order with open status
      - `|`: OR condition: Ex: `"FIELDBUDDY__Work_Order_Line_Item__c[*FIELDBUDDY__Status__c=Open|FIELDBUDDY__Status__c=Closed]"`  -> Returns multiple Work Order Line Items with status open or closed
      - `!`: NEG condition Ex: `"FIELDBUDDY__Work_Order_Line_Item__c[*FIELDBUDDY__Status__c!=Closed]"` -> Returns multiple Work Order Line Item with status different to Closed
  3. **Helpers**  
There are some extra custom functions that we can attach to the formulas.
    - `:isServiceRequestBased` → returns true if given work order is  service request based
    - `:isLocationBased` → returns true if given work order is location-based
    - `:isEmpty`-> returns true if collection is empty
    - `:isNotEmpty` → returns true if collection is not empty  
For example: `FIELDBUDDY__Work_Order__c[Id=${CURRENT_WORK_ORDER}]:isServiceRequestBased` -> Returns true if the current Work Order  is a service-request based.
  4. **Constants**  
If a constant is mentioned inside of a formula. The code will replace it will the correct value.
    1. `${CURRENT_WORK_ORDER}` - can be used inside of a formula as a reference to the current work order being displayed.
    2. `${CURRENT_ID}` = can be used inside of a formula as a reference to the main object in the screen.
    3. `${CURRENT_IP_ID}` - can be used inside of a formula as a reference to the Installed Product being displayed.
      

### Important Considerations

> ⚠️ Some of types of the fields (for example text or picklist fields that have empty spaces in values) or numeric fields can not be used reliably withing the JsonQuery, so it makes sense to use another types of validations ([https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5381455880](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5381455880) or [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5985304580](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5985304580) )

### Examples

| **Other Examples** | **Description** |
| --- | --- |
| `FIELDBUDDY__Work_Order_Line_Item__c[FIELDBUDDY__Work_Order__c=${CURRENT_WORK_ORDER}&Type__c=Activity]` | returns **true** if there is at least one WOLI with type Activity in the current work order |
| `FIELDBUDDY__Work_Order_Line_Item__c[FIELDBUDDY__Work_Order__c=${CURRENT_WORK_ORDER}&Type__c=Activity]:isEmpty` | returns **true** if there are no WOLIs with type Activity in the current work order |
| `FIELDBUDDY__Work_Order_Line_Item__c[FIELDBUDDY__Work_Order__c=${CURRENT_WORK_ORDER}&Status__c=Open]:isNotEmpty` | returns **true** if there is at least one WOLI with status Open in the current work order |
| `FIELDBUDDY__Work_Order__c[*FIELDBUDDY__Status__c=Open]` | returns **true** if there is at least one WO  the status Open |

> ℹ️ It is good idea to always end formula with isEmpty or isNotEmpty in order to avoid ambiguity

## Usage

JSON-query like formulas can be used in Filter Logic and in the Validations. Both share the **executor**, **source** and **formula** properties, but have different** type** or extra properties

### **Filter Logic**

#### Definition

Filter Logic will check if the formula returns something or not. If there is a result it will return true and false if the formula fails.

This can be used to hide components and logic rules that depends on the current data values.

| **Parameter** | **Description** |
| --- | --- |
| `executor` | "jsonQuery" |
| `type` | "filter" |
| `source` | "${DATA}" or "${MEDIA_LIBRARY}" |
| `formula` | Contains the validation rule formula as described above |

#### Examples

Checks if Record Type name of a current Work Order contains `Automatische_brandblusser`

```
"condition": [
    {
        "executor": "jsonQuery",
        "type": "filter",
        "source": "${DATA}",
        "formula": "FIELDBUDDY__Work_Order__c[Id=${CURRENT_WORK_ORDER}].RecordType.DeveloperName:contains(Automatische_brandblusser)"
    }
]
```

### Combining Conditions

It is also possible to combine condition with AND or OR Statement  
For example:

```
    "condition": {
        "conditions": [
            {
                "executor": "jsonQuery",
                "type": "filter",
                "source": "${DATA}",
                "formula": "FIELDBUDDY__Work_Order__c[Id=${CURRENT_WORK_ORDER}&FIELDBUDDY__Closure_Status__c=SITE_NOT_READY]:isNotEmpty"
            },
            {
                "executor": "jsonQuery",
                "type": "filter",
                "source": "${DATA}",
                "formula": "FIELDBUDDY__Work_Order__c[Id=${CURRENT_WORK_ORDER}&FIELDBUDDY__Closure_Status__c=SITE_NOT_READY_WITHOUT_CUSTOMER_ACCEPTANCE]:isNotEmpty"
            }
        ],
        "operator": "OR"
    }
```

Show price (`FIELDBUDDY__Unit_Price_Including_Tax_input__c`) if and only if if parent work order has checkbox `Is_Business_Account__c` as true

```
{
    "headless": true,
    "table": "FIELDBUDDY__Work_Order_Line_Item__c",
    "iconName": "md-person",
    "iosIconName": "ios-person",
    "mapping": "FIELDBUDDY__Work_Order_Line_Item__c",
    "condition": [
        {
            "executor": "jsonQuery",
            "type": "filter",
            "source": "${DATA}",
            "formula": "FIELDBUDDY__Work_Order__c[Id=${CURRENT_WORK_ORDER}&Is_Business_Account__c=true]:isNotEmpty"
        }
    ],
    "title": "",
    "objects": [
        {
            "name": "FIELDBUDDY__Unit_Price_Including_Tax_input__c"
        }
    ]
}
```

### Validation Rules

#### Definition

The main difference between Validation Rules and Filter Logic is that Validation Rules also have some side effects. We can stop the flow of an event if the formula fails, we can show an error message, etc.

Validation Rules have 2 extra fields in the CFG file. Alert Type and AlertMessage. Which rules how the validation logic will behave.

Below is the basic description of the properties that has to be specified for given types of validation rules

| **Parameter** | **Description** |
| --- | --- |
| `executor` | "jsonQuery" |
| `type` | "validation" |
| `alertType` | "error" or "warning" |
| `source` | "${DATA}" or "${MEDIA_LIBRARY}" |
| `formula` | Contains the validation rule formula as described above |
| `alertMessage` | This is the message the user will see when the validation fails. It can be a static text or a custom label |
| `alertDefaultText` | It is displayed, if alertMessage is a custom label but it can’t be found |

#### Example

Warning will be shown in Work Order closure if there are no WOLI with Type Activity

```
{
    "validation": [
        {
            "executor": "jsonQuery",
            "type": "validation",
            "alertType": "warning",
            "source": "${DATA}",
            "alertMessage": "${Label.NoActivities}",
            "formula": "FIELDBUDDY__Work_Order_Line_Item__c[*FIELDBUDDY__Work_Order__c=${CURRENT_WORK_ORDER}&FIELDBUDDY__Type__c=Activity]"
        }
    ]
}
```

More of examples can be found here - [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5985304580](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5985304580)