---
title: "Validation Rules using JS engine"
canonical: "https://help.fieldbuddy.com/space/FBDOCS/5381455880/Validation%20Rules%20using%20JS%20engine"
format: markdown
---
> ℹ️ You can see some examples [in this directory](https://help.fieldbuddy.com/space/FBDOCS/5985140739/Validation+and+Filter+Logic+using+JS+engine).

Validation Rules using JS engine allows more powerful tweaking of validation rules that can gather the data from multiple objects.

It requires `supportValidationRules` in app settings / otherDefaults ( [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5671190556](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5671190556) ).

It should be placed in array `validationRules` in the `WorkOrders` app or another one if used. 

Sample format

```
{
    "Id": "DR-47_YES",
    "Name": "MyFormula2__f DR-47 - YES",
    "Code__c": null,
    "Error_Message__c": "Vooraad gestuurd",
    "SwiftFormula": {
        "params": [
            {
                "var": "${CURRENT_WORK_ORDER_ID}",
                "constant": "${Constants.CURRENT_WORK_ORDER_ID}"
            },
            {
                "var": "${MAGAZIJNEN}",
                "field": "Magazijnen_voorraadgestuurd__c",
                "table": "FIELDBUDDY__Work_Order_Line_Item__c"
            }
        ],
        "helpers": [
            {
                "context": "GLOBAL",
                "method": "_.first",
                "select": "Magazijncode_monteur__c",
                "from": "FIELDBUDDY__Work_Order__c",
                "where": {
                    "fields": [
                        "Id"
                    ],
                    "values": [
                        "${CURRENT_WORK_ORDER_ID}"
                    ]
                },
                "var": "${CODE_MONTEUR}"
            }
        ],
        "formula": "f=()=>{var codeMonteur =`${CODE_MONTEUR}`;var magazijnen=`${MAGAZIJNEN}`;var res='';if (codeMonteur == null || magazijnen == null) {res = 'Nee';} else if (magazijnen.includes(codeMonteur)) {res = 'Ja';} else {res = 'Nee'};return (res==='Ja'); };Boolean(f());"
    }
}
```

### Properties

1. `Id` – used when you refer formula in Swift config, so should be unique
2. `Name` - used in testing/debugging, should be unique
3. `Error_Message__c` - message of text that is being shown
4. `SwiftFormula` - determines formula itself. if it is true → it is preventing sometihng or section is hidden
  1. params – reference to fields used
  2. helpers - ability to call another functions. Check here for a full list - [https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5382144004](https://upperdeck.atlassian.net/wiki/spaces/FBDOCS/pages/5382144004)
  3. formula - JS that evaluates as formula
5. `Code__c` → used to determine when formula is called. Possible values are
  1. `BEFORECLOSURE` - runs BEFORE work order is closed, so before closure screen
  2. `BEFOREDELETE` - runs before deletion of WOLI, then `Mobile_Page__r` has to be the `Work_Order_Line_Item__c`
  3. or empty - then `Mobile_Page__r`is checked
6. `Mobile_Page__r` allows to determine where the validation rule should be executed. Possible options are
  1. `Timesheet Lineitem` - on create/edit on timesheet line item
  2. `Work Order Closure` - on Work Order Closure screen (before Confirm button)
  3. `Edit Work Order Other Expenses` - on create/ edit of WOLI type Other expense
  4. `Edit Work Order Parts` - on create/edit of WOLI type Part
  5. `Edit Work Order Activities` - on create/edit of WOLI type Activity

### Important Considerations

1. It makes sense to use a ``` character while referring fields values instead of `'`, since otherwise end of the line symbol might not be encoded and validation might now work as expected.
2. In order to do debugging – you can use the “Check Validation Rule Assertions” in the dev/debug menu. This menu will by default show 10 last evaluated validation rules, but this number can be adjusted using the [validationRuleEvaluationStorageLimit](https://help.fieldbuddy.com/space/FBDOCS/5671190556/otherDefaults) config setting.
  1. If you do not know how to access this menu, get in touch with a member of the FieldBuddy Pro Dev Team.

![ScreenRecording_09-11-2026 15-35-57_1.mov](media://45a314ae-aa38-4a4f-898c-4a26e5627536)


### Example validations

1. Prevent TSLI to be saved if end km is bigger then start km on odometer
2. Prevent WO closure with status solved if type is Inspection
3. Prevent closing WO if closure status is FOLLOW_UP and comments from technicians are empty

More of the examples can be found here -