logo

NJP

Validate Decision Table result values using Instance Scan

Import · Dec 06, 2021 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

Hi there,

Decision Tables was the main subject in recent Platform Academy and Creator Toolbox episodes. Decision Tables was clearly explained and demoed. Below are the links to both episodes.

- 2021-11-11: Platform Academy Session #10: Getting Started with Decision Builder

- 2021-11-12: Creator Toolbox - Decision Builder with Julia Perlis

A question that did pop-up during the Creator Toolbox episode, how to track when one of the conditions gets deactivated. It doesn't nessesaraly have to be an issue, the next decision will just be evaluated. Though what if there are no matching conditions anymore because of this, or what if the defined result is deactivated, that could potentially cause an issue. My thought immediately, we might run Instance Scan on this image.

In two articles I'll dive into creating Scan Checks to check for the condition, and to check for the result. In this first article, let's get after checking the result!

Decision Tables

I've simulated the example from the Creator Toolbox episode as much as possible. Within the Decision Builder it looks something like:

image

What we are after, is finding out if Instance Scan can scan for the result "Group". If it's inactive or it doesn't exist, creating a Scan Finding.

Decision Question

After searching a bit, I found a table "Decision" [sys_decision_question]. This table holds an "answer" field which holds the value that we are after.

image

Decision Table result is inactive or does not exist

Knowing this table structure, we can start with building Scan Checks. Scanning for the result looks easy. Creating a Table Scan Check, selecting the Table, adding conditions, adding script because a Document ID field is used. With this we can quickly create a Scan Check to see if the result value exists. To also check if it's active or not is a bit more work. Not every table has an "active" field, and for this Scan Check I'm just ignoring that sys_choice actually has an "inactive" field or custom tables might have an "u_active" field.

Table Check

Table:

sys_decision_question

Conditions:

active=trueanswer!=decision_tableISNOTEMPTY

Script:

(function (engine) {

    // Define variables
    var table_name = engine.current.decision_table.answer_table.toString();

    // Check if table has active field
    var grActiveCheck = new GlideRecord(table_name);
    grActiveCheck.initialize();

    var fieldSize = grActiveCheck.getElements().size();
    var fieldsArr = grActiveCheck.getElements(); 

    var hasActiveField = false;
    for(var i = 0; i < fieldSize && !hasActiveField; i++) {
        var fieldNameStr = fieldsArr.get(i).getName().toString();
        var fieldTypeStr = fieldsArr.get(i).getED().getInternalType().toString();

        hasActiveField = fieldNameStr == 'active' && fieldTypeStr == 'boolean';
    }

    // Get record
    var grResultRecord = new GlideRecord(table_name);
    grResultRecord.addQuery('sys_id', engine.current.answer.toString());
    if(hasActiveField) {
        grResultRecord.addActiveQuery();
    }
    grResultRecord.setLimit(1);
    grResultRecord._query();

    // Create scan finding
    if(grResultRecord._next()) {
        engine.finding.increment();
    }

})(engine);

---

View original source

https://www.servicenow.com/community/developer-articles/validate-decision-table-result-values-using-instance-scan/ta-p/2323672