GlideQuery - スコープ対象、グローバル

  • リリースバージョン: Xanadu
  • 更新日 2024年08月01日
  • 所要時間:59分
  • GlideQuery スクリプトインクルードは、サーバーサイドスクリプトからレコードデータに対して CRUD 操作を実行するための GlideRecord API の代替です。

    GlideQuery スクリプトインクルードを使用すると、次のことができます。

    • クエリと結果には標準の JavaScript オブジェクトとタイプを使用します。
    • 追加のチェックでクエリエラーを迅速に診断し、エラーメッセージをクリアします。
    • 定型的なクエリパターンを避けてコードを簡素化します。
    • GlideRecord に関する深い知識がなくても、一般的なパフォーマンスの問題を回避できます。
    スコープ対象またはグローバルのサーバーサイドスクリプトで GlideQuery スクリプトインクルードを使用します。スコープ対象のアプリ内で使用する場合は、グローバルスコープをプリフィックスとして付ける必要があります。
    new global.GlideQuery('sys_user')
    // ...
    このスクリプトインクルードには GlideQuery [com.sn_glidequery] プラグインが必要です。

    実装

    このスクリプトインクルードは、メソッド呼び出しが連鎖していて、各メソッドが前のメソッドで返された結果に基づいて構築されるビルダーパターン。メソッドを使用してクエリの属性を定義します。メソッドは、ターミナルメソッド (クエリ結果を返すメソッド) を呼び出すまで実行されないため、クエリを実行する前にクエリの要件を定義できます。

    クエリが単一のレコードを返す場合、システムは Optional オブジェクトに結果をラップします。クエリがレコードのストリームを返す場合、システムは Stream オブジェクトに結果をラップします。これらのオブジェクトを使用すると、各 API で一連のメソッドを使用して結果を管理できます。

    たとえば、この スクリプトはタスクテーブルでクエリを実行し、レコードを優先度別にグループ化して、合計再アサイン回数が 4 を超える各優先度を返します。

    var query = new global.GlideQuery('task')
        .where('active', true) //Returns new GlideQuery object with a "where" clause.
        .groupBy('priority') //Returns new GlideQuery object with a "group by" clause.
        .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a "sum(reassignment_count)" clause.
        .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a "having reassignment_count > 4" clause.
        .select() //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. 
          gs.info("Priority " + priority.group.priority + ": " + priority.sum.reassignment_count + " reassignments");
        });
    出力:
    Priority 1: 11 reassignments
    Priority 3: 6 reassignments
    Priority 5: 5 reassignments

    エラー処理

    GlideQuery スクリプトインクルードは、クエリに問題がある場合にエラーをスローし、ガイドとなる明確な説明を含めます。このスクリプトには、次のチェックが含まれています。

    • 無効なフィールド
    • フィールドの無効な値タイプ
    • 選択フィールドの無効な値
    • 無効なクエリ演算子

    たとえば、次のコード サンプルでは、クエリされたフィールドがテーブルに存在しないため、エラーがスローされます。

    new global.GlideQuery('task')
        .where('id', '4717dfe5a9fe198100450448b2404c16') // should be 'sys_id'
        .select('description', 'severity')
        .toArray(100);
      // Error: Unable to find field 'id' in table 'task'. Known fields: active, activity_due, ...

    いずれか 1 つの引数のデータタイプが正しくないため、このコードサンプルはエラーをスローします。

    new global.GlideQuery('task')
        .where('priority', 'one') // priority is an integer (should be 1)
        .select('description', 'severity')
        .toArray(100);
      // Error: Unable to match value ['one'] with field 'priority' in table 'task'. Expecting type 'integer'

    再利用

    GlideQuery オブジェクトは不変であるため、後でコードの他の部分で再利用できます。たとえば、このスクリプトはクエリを作成し、後で GlideQuery オブジェクトを使用してレポートを生成します。

    var highPriorityTasks = new global.GlideQuery('task')
        .where('active', true)
        .where('priority', 1);
    
    generateReport(highPriorityTasks);
    notifyOwners(highPriorityTasks);
    var avgReassignmentCount = highPriorityTasks
        .avg('reassignment_count')
        .orElse(0)
    

    制限事項

    GlideQuery スクリプトインクルードは以下をサポートしていません。

    • 他のスコープからのアクセスを許可しないテーブルの読み取りまたは書き込み。
    • エンコードクエリの読み取り。
    • JavaScript 文字列として読み取られる GlideDate または GlideDateTime オブジェクト。
    • [FX 通貨] フィールド。
    • ジャーナルフィールドタイプの更新。
    • あいまいな条件付きロジックを含むクエリ。たとえば、次のクエリは (active = true AND name != null) OR last_name = Luddy または active = true AND (name != null OR last_name = Luddy) のどちらを実行するのかわからないため、不明確です。
      var user = new global.GlideQuery('sys_user')
        .where('active', true)
        .whereNotNull('name')
        .orWhere('last_name', 'Luddy')
        .selectOne()
        .get()

      代わりに子クエリをネストする方法については、where() メソッドを参照してください。

    注:
    GlideQuery スクリプトインクルードは GlideRecord オブジェクトを標準の JavaScript オブジェクトに変換するため、クエリの実行に時間がかかる場合があります。パフォーマンスの問題を軽減するには、多数のレコードを繰り返すループを作成しないようにしてください。

    GlideQuery の中間メソッドとターミナルメソッド

    GlideQuery スクリプトインクルードは、中間とターミナルの 2 つのカテゴリのメソッドを使用します。中間メソッドは、レコードなどのアイテムのストリームと対話するために使用される API である Stream を返すメソッドであり、呼び出しが連鎖する流暢な構文スタイルを可能にします。ターミナル・メソッドは、ストリームを返さないため、 Stream メソッド呼出しのチェーンを停止するメソッドです。

    GlideQuery スクリプトインクルードでは、where()、orderBy()、disableWorkflow() などのメソッドは、新しい GlideQuery オブジェクトを返す中間メソッドです。同様に、GlideQuery スクリプトインクルードの最も一般的なターミナルメソッドである select() および selectOne() もターミナルメソッドです。これらのメソッドは、 GlideQuery の構成が完了し、レコードの処理を開始する準備ができたときに呼び出されます。特定のユースケースに従って GlideQuery スクリプトインクルードの呼び出しを構成する場合は、これら 2 つのメソッドタイプの違いに留意することが重要です。中間メソッドとターミナルメソッドの詳細については、「 GlideQuery - ストリーム処理 第 1 部」の記事を参照してください。

    次のメソッドは中間として分類されます。

    • aggregate(文字列 aggregateType, 文字列 field)
    • disableAutoSysFields()
    • disableWorkflow()
    • forceUpdate()
    • groupBy(文字列 fields)
    • having(文字列 aggregateType, 文字列 field, 文字列 operator, 数値 value)
    • limit(数値 limit)
    • orderBy(文字列 fields)
    • orderByDesc(文字列 fieldOrAggregate, 文字列 field)
    • orWhere(文字列 fieldOrQuery, 文字列 operator, 任意 value)
    • orWhereNotNull(文字列 field)
    • orWhereNull(文字列 field)
    • where(文字列 fieldOrQuery, 文字列 operator, 任意 value)
    • whereNotNull(文字列 field)
    • whereNull(文字列 field)
    • withAcls()

    次のメソッドはターミナルとして分類されます。

    • avg(文字列 field)
    • count()
    • deleteMultiple()
    • get(文字列 key, アレイ selectedFields)
    • getBy(オブジェクト keyValues, アレイ selectedFields)
    • insert(オブジェクト keyValues, オブジェクト selectedFields)
    • insertOrUpdate(オブジェクト changes, オブジェクト selectedFields)
    • max(文字列 field)
    • min(文字列 field)
    • select(文字列 fields)
    • selectOne(文字列 fields)
    • sum(文字列 field)
    • toGlideRecord()
    • update(オブジェクト changes, オブジェクト selectedFields)
    • updateMultiple(オブジェクト changes)
    注:
    parse(String table, String encoded_query) は中間でも終端でもなく、静的です。

    GlideQuery - GlideQuery(文字列 table)

    レコードクエリの構築と実行に使用される GlideQuery オブジェクトをインスタンス化します。

    表 : 1. パラメーター
    名前 タイプ 説明
    table 文字列 クエリ対象のテーブル。
    表 : 2. 返される内容
    タイプ 説明
    なし

    この例では、ユーザーテーブルのクエリをインスタンス化します。

    var query = new global.GlideQuery('sys_user');

    GlideQuery - aggregate(文字列 aggregateType, 文字列 field)

    指定された集計関数を使用してフィールドを集計します。

    このメソッドは、複数のフィールドに対して集計するクエリを構築する場合、または複数の集計関数を使用する場合、または groupBy() メソッドを使用する必要がある場合に使用します。1 つの関数で 1 つのフィールドに対してのみ集計する場合で、groupBy() を使用する必要がない場合は、代わりに次のいずれかのメソッドを使用します。

    • avg()
    • min()
    • max()
    • count()
    表 : 3. パラメーター
    名前 タイプ 説明
    aggregateType 文字列 実行する集計関数のタイプ。次のオプションが含まれます。
    • min:一致するすべてのレコードの最小値を返します。
    • max:一致するすべてのレコードの最大値を返します。
    • sum:一致するすべてのレコードの合計を返します。
    • avg:一致するすべてのレコードの平均を返します。
    • count:一致するレコードの数を返します。
    field 文字列 操作を実行するフィールド。
    表 : 4. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。
    この スクリプトはタスクテーブルでクエリを実行し、レコードを優先度別にグループ化して、合計再アサイン回数が 4 を超える各優先度を返します。
    var query = new global.GlideQuery('task')
        .where('active', true) //Returns new GlideQuery object with a "where" clause.
        .groupBy('priority') //Returns new GlideQuery object with a "group by" clause.
        .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a "sum(reassignment_count)" clause.
        .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a "having reassignment_count > 4" clause.
        .select() //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. 
          gs.info("Priority " + priority.group.priority + ": " + priority.sum.reassignment_count + " reassignments");
        });
    出力:
    Priority 1: 11 reassignments
    Priority 3: 6 reassignments
    Priority 5: 5 reassignments

    GlideQuery - avg(文字列 field)

    指定された数値フィールドの集計平均を返します。

    このメソッドは、次のタイプのフィールドでのみ使用できます。
    • 整数
    • 浮動小数点数
    • 倍精度
    • 通貨
    表 : 5. パラメーター
    名前 タイプ 説明
    field 文字列 操作を実行するフィールド。
    表 : 6. 返される内容
    タイプ 説明
    オプション 指定されたフィールドの集計平均を含むオブジェクト。

    この例は、cmdb_ci テーブルの障害の平均数を返す方法を示しています。

    var faults = new global.GlideQuery('cmdb_ci')
        .avg('fault_count')
        .orElse(0);
    
    gs.info(JSON.stringify(faults));

    出力:

    0.0037

    GlideQuery - count()

    クエリに一致するレコードの数を返します。

    表 : 7. パラメーター
    名前 タイプ 説明
    なし
    表 : 8. 返される内容
    タイプ 説明
    数値 クエリに一致するレコードの数。

    この例では、ユーザーテーブル内のアクティブなレコードの数を返します。

    var userCount = new global.GlideQuery('sys_user')
        .where('active', true)
        .count();

    出力:

    612

    GlideQuery - deleteMultiple()

    先行する where 句で指定されたテーブルにあるすべてのレコードを削除します。

    表 : 9. パラメーター
    名前 タイプ 説明
    なし
    表 : 10. 返される内容
    タイプ 説明
    なし

    この例では、ユーザーテーブルで姓が Jeter であるアクティブなすべてのレコードを削除します。

    var query = new global.GlideQuery('sys_user')
        .where('active', true)
        .where('last_name', 'Jeter')
        .deleteMultiple();

    GlideQuery - disableAutoSysFields()

    システムフィールド、または sys_created_on、sys_updated_on、sys_mod_count などの sys プリフィックスで始まる名前のフィールドの更新を無効にします。指定されたクエリにのみ適用されます。

    表 : 11. パラメーター
    名前 タイプ 説明
    なし
    表 : 12. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例ではタスクテーブルにレコードを追加しますが、システムフィールドは設定しません。このメソッドを呼び出さない場合、以下の例では sys_updated_on、sys_mod_count などが更新されます。

    var query = new global.GlideQuery('task')
        .disableAutoSysFields()
        .insert({ description: 'example', priority: 1 });

    GlideQuery - disableWorkflow()

    クエリの結果として実行または作成されるビジネスルール、フロー、ワークフロー、または監査レコードを無効にします。

    表 : 13. パラメーター
    名前 タイプ 説明
    なし
    表 : 14. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例では、自動ビジネスプロセスをトリガーせずにタスクテーブルの複数のレコードを更新します。

    var query = new global.GlideQuery('task')
        .disableWorkflow()
        .where('active', true)
        .updateMultiple({ priority: 1 });

    GlideQuery - forceUpdate()

    レコードが変更されていない場合でも、データベースの更新を強制します。たとえば、このメソッドを使用してビジネスルールを強制的に実行できます。

    表 : 15. パラメーター
    名前 タイプ 説明
    なし
    表 : 16. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例では、特定の sys_id でタスクレコードを強制的に更新します。

    var forceUpdate = new global.GlideQuery('task')
        .forceUpdate()
        .where('sys_id', taskId)
        .update()

    GlideQuery - get(文字列 key, アレイ selectedFields)

    クエリから単一のレコードを返します。

    表 : 17. パラメーター
    名前 タイプ 説明
    key 文字列 返されるレコードの sys_id。
    selectedFields アレイ オプション。結果で返す追加フィールド。

    デフォルト:システムは常に sys_id を返します。

    表 : 18. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    sys_id に基づいてレコードを返す例。

    var user = new global.GlideQuery('sys_user')
        .get('5137153cc611227c000bbd1bd8cd2005', ['first_name', 'last_name']) //Returns an Optional object.
        .orElse({ first_name: 'Default', last_name: 'User' }); //Method in the Optional class to return a default value.
    
    gs.info(JSON.stringify(user, null, 2));

    出力:

    {
       "sys_id":"5137153cc611227c000bbd1bd8cd2005",
       "first_name":"Fred",
       "last_name":"Luddy"
    }

    GlideQuery - getBy(オブジェクト keyValues, アレイ selectedFields)

    クエリの基準となる一連の名前と値のペアに基づいて、単一のレコードを含むオプションのオブジェクトを返します。名前と値のペアごとに「=」演算子を想定します。

    表 : 19. パラメーター
    名前 タイプ 説明
    keyValues オブジェクト キーがフィールドの名前であり、値がクエリの対象であるオブジェクト。
    selectedFields アレイ オプション。結果で返す追加フィールド。

    デフォルト:システムは常に sys_id を返します。

    表 : 20. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    ユーザーの名前をクエリしてレコードを返す例。

    var user = new global.GlideQuery('sys_user')
        .getBy({
            first_name: 'Fred',
            last_name: 'Luddy'
        }, ['first_name', 'last_name', 'city', 'active']) // select first_name, last_name, city, active
        .orElse({
            first_name: 'Nobody',
            last_name: 'Found',
            city: 'Nowhere',
            active: false
        });
    
    gs.info(JSON.stringify(user, null, 2));

    出力:

    {
       "first_name":"Fred",
       "last_name":"Luddy",
       "city":null,
       "active":true,
       "sys_id":"5137153cc611227c000bbd1bd8cd2005"
    }

    GlideQuery - groupBy(文字列 fields)

    指定されたフィールド別にクエリ結果をグループ化します。

    このメソッドは aggregate() メソッドとともに使用する必要があります。

    表 : 21. パラメーター
    名前 タイプ 説明
    fields 文字列または文字列のアレイ 結果をグループ化するフィールド。
    表 : 22. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。
    この スクリプトはタスクテーブルでクエリを実行し、レコードを優先度別にグループ化して、合計再アサイン回数が 4 を超える各優先度を返します。
    var query = new global.GlideQuery('task')
        .where('active', true) //Returns new GlideQuery object with a "where" clause.
        .groupBy('priority') //Returns new GlideQuery object with a "group by" clause.
        .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a "sum(reassignment_count)" clause.
        .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a "having reassignment_count > 4" clause.
        .select() //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. 
          gs.info("Priority " + priority.group.priority + ": " + priority.sum.reassignment_count + " reassignments");
        });
    出力:
    Priority 1: 11 reassignments
    Priority 3: 6 reassignments
    Priority 5: 5 reassignments

    GlideQuery - having(文字列 aggregateType, 文字列 field, 文字列 operator, 数値 value)

    指定した条件に一致する結果のグループのみを表示できるように、集計グループをフィルタリングします。

    このメソッドは aggregate() メソッドまたは groupBy() メソッドとともに使用する必要があります。

    表 : 23. パラメーター
    名前 タイプ 説明
    aggregateType 文字列 実行する集計関数のタイプ。次のオプションが含まれます。
    • min:一致するすべてのレコードの最小値を返します。
    • max:一致するすべてのレコードの最大値を返します。
    • sum:一致するすべてのレコードの合計を返します。
    • avg:一致するすべてのレコードの平均を返します。
    • count:一致するレコードの数を返します。
    field 文字列 操作を実行するフィールド。
    operator 文字列 操作で使用する数値演算子。
    次のオプションが含まれます。
    • >:次の値を超える
    • <:次の値未満
    • >=:次の値以上
    • <= :次の値以下
    • = :次の値に等しい
    • !=:次の値と等しくない
    value 数値 操作で使用する数値。
    表 : 24. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。
    この スクリプトはタスクテーブルでクエリを実行し、レコードを優先度別にグループ化して、合計再アサイン回数が 4 を超える各優先度を返します。
    var query = new global.GlideQuery('task')
        .where('active', true) //Returns new GlideQuery object with a "where" clause.
        .groupBy('priority') //Returns new GlideQuery object with a "group by" clause.
        .aggregate('sum', 'reassignment_count') //Returns new GlideQuery object with a "sum(reassignment_count)" clause.
        .having('sum', 'reassignment_count', '>', 4) //Returns new GlideQuery object with a "having reassignment_count > 4" clause.
        .select() //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (priority){ //Terminal method in the Stream class that executes the query and returns the result. 
          gs.info("Priority " + priority.group.priority + ": " + priority.sum.reassignment_count + " reassignments");
        });
    出力:
    Priority 1: 11 reassignments
    Priority 3: 6 reassignments
    Priority 5: 5 reassignments

    GlideQuery - insert(オブジェクト keyValues, オブジェクト selectedFields)

    レコードを挿入し、そのレコードを含むオプションのオブジェクトを返します。

    表 : 25. パラメーター
    名前 タイプ 説明
    keyValues オブジェクト レコードに挿入する名前と値のペアを含むオブジェクト。未指定のフィールドは null になります。
    selectedFields アレイ オプション。結果で返す追加フィールド。

    デフォルト:システムは常に sys_id を返します。

    表 : 26. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、ユーザーの姓と名に基づいてレコードを挿入する方法を示しています。

    var fred = new global.GlideQuery('sys_user')
        .insert({ first_name: 'Fred', last_name: 'Luddy' })
        .get();
    
    gs.info(JSON.stringify(fred, null, 2));

    出力:

    {
       "sys_id":"cf16eed0e82a9010f8778bda83d255d2",
       "first_name":"Fred",
       "last_name":"Luddy"
    }

    GlideQuery - insertOrUpdate(オブジェクト changes, オブジェクト selectedFields)

    既存のレコードを更新するか、まだ存在しない場合は新しいレコードを挿入します。

    表 : 27. パラメーター
    名前 タイプ 説明
    changes オブジェクト 更新またはレコードに挿入する名前と値のペアを含むオブジェクト。
    selectedFields アレイ オプション。結果で返す追加フィールド。

    デフォルト:システムは常に sys_id を返します。

    表 : 28. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、システムに存在しない新しいレコードを挿入する方法を示しています。

    // insert a new record
    var user = new GlideQuery('sys_user')
        .insertOrUpdate({
            first_name: 'George',
            last_name: 'Griffey'
        })
        .orElse(null);

    この例は、既存のレコードを更新する方法を示しています。

    // update existing record
    var user = new global.GlideQuery('sys_user')
        .insertOrUpdate({
            sys_id: '2d0efd6c73662300bb513198caf6a72e',
            first_name: 'George',
            last_name: 'Griffey' })
        .orElse(null);

    GlideQuery - limit(数値 limit)

    クエリで返されるレコードの数を制限します。

    表 : 29. パラメーター
    名前 タイプ 説明
    limit 数値 返されるレコードの数。
    表 : 30. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、返される結果を 5 つのレコードに制限する方法を示しています。

    var incidents = new global.GlideQuery('incident')
        .limit(5)
        .select('priority', 'description')
        .forEach(function (incident){
            gs.info(JSON.stringify(incident, null, 2));
        });

    出力:

    *** Script: {
      "priority": 1,
      "description": "User can't get to any of his files on the file server.",
      "sys_id": "9d385017c611228701d22104cc95c371"
    }
    *** Script: {
      "priority": 1,
      "description": "I just moved from floor 2 to floor 3 and my laptop cannot connect to any wireless network.",
      "sys_id": "e8caedcbc0a80164017df472f39eaed1"
    }
    *** Script: {
      "priority": 1,
      "description": "User forgot their email password.",
      "sys_id": "9d3c1197c611228701cd1d94bc32d76d"
    }
    *** Script: {
      "priority": 1,
      "description": "When I try to print, my whole computer just freezes and stops working.",
      "sys_id": "8d6246c7c0a80164012fb063cecd4ace"
    }
    *** Script: {
      "priority": 3,
      "description": "Unable to login even though login credentials are correct.",
      "sys_id": "a9e30c7dc61122760116894de7bcc7bd"
    }

    GlideQuery - max(文字列 field)

    指定されたフィールドの集計最大を返します。

    表 : 31. パラメーター
    名前 タイプ 説明
    field 文字列 操作を実行するフィールド。
    表 : 32. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、指定されたフィールドの最大値または最大の英数字値を返す方法を示しています。

    var name = new global.GlideQuery('sys_user')
        .max('last_name')
        .orElse('');
    
    gs.info(JSON.stringify(name));

    出力:

    "Zortman"

    GlideQuery - min(文字列 field)

    指定されたフィールドの集計最小を返します。

    表 : 33. パラメーター
    名前 タイプ 説明
    field 文字列 操作を実行するフィールド。
    表 : 34. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、指定されたフィールドの最小値または最小の英数字値を返す方法を示しています。

    var name = new global.GlideQuery('sys_user')
        .min('last_name')
        .orElse('');
    
    gs.info(JSON.stringify(name));

    出力:

    "Abel"

    GlideQuery - orderBy(文字列 fields)

    返された結果を指定されたフィールド別に昇順で並べます。

    表 : 35. パラメーター
    名前 タイプ 説明
    fields 文字列 結果を昇順で並べ替えるためのカンマ区切りフィールド。
    表 : 36. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、結果をレコード番号で昇順に並べ替える方法を示しています。

    var query = new global.GlideQuery('incident')
        .orderBy('number')
        .limit(5)
        .select('number', 'description') //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (incident){ //Terminal method in the Stream class that executes the query and returns the result.
            gs.info(JSON.stringify(incident, null, 2));
        });

    出力:

    *** Script: {
      "number": "INC0000001",
      "description": "User can't access email on mail.company.com.",
      "sys_id": "9c573169c611228700193229fff72400"
    }
    *** Script: {
      "number": "INC0000002",
      "description": "User can't get to any of his files on the file server.",
      "sys_id": "9d385017c611228701d22104cc95c371"
    }
    *** Script: {
      "number": "INC0000003",
      "description": "I just moved from floor 2 to floor 3 and my laptop cannot connect to any wireless network.",
      "sys_id": "e8caedcbc0a80164017df472f39eaed1"
    }
    *** Script: {
      "number": "INC0000004",
      "description": "User forgot their email password.",
      "sys_id": "9d3c1197c611228701cd1d94bc32d76d"
    }
    *** Script: {
      "number": "INC0000005",
      "description": "CPU was 100% busy for more than 10 minutes",
      "sys_id": "e8e875b0c0a80164009dc852b4d677d5"
    }

    GlideQuery - orderByDesc(文字列 fieldOrAggregate, 文字列 field)

    返された結果を指定されたフィールド別に降順で並べます。

    表 : 37. パラメーター
    名前 タイプ 説明
    fieldOrAggregate 文字列 クエリで aggregate() メソッドを使用しない場合は、結果を並べ替えるフィールドを渡します。

    クエリで aggregate() メソッドを使用する場合は、実行する集計関数のタイプを渡します。

    次のオプションが含まれます。
    • min:一致するすべてのレコードの最小値を返します。
    • max:一致するすべてのレコードの最大値を返します。
    • sum:一致するすべてのレコードの合計を返します。
    • avg:一致するすべてのレコードの平均を返します。
    • count:一致するレコードの数を返します。
    field 文字列 オプション。結果を降順で並べ替えるためのフィールド。aggregate() メソッドを使用するクエリに必要です。
    表 : 38. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、結果を番号で降順に並べ替える方法を示しています。

    var query = new global.GlideQuery('incident')
        .orderByDesc('number')
        .limit(5)
        .select('number', 'description') //Returns a stream of records wrapped in a Stream object.  
        .forEach(function (incident){ //Terminal method in the Stream class that executes the query and returns the result.
            gs.info(JSON.stringify(incident, null, 2));
        });

    出力:

    *** Script: {
      "number": "INC0009009",
      "description": "Unable to access the shared folder. Please provide access.",
      "sys_id": "57af7aec73d423002728660c4cf6a71c"
    }
    *** Script: {
      "number": "INC0009005",
      "description": "Unable to send or receive emails.",
      "sys_id": "ed92e8d173d023002728660c4cf6a7bc"
    }
    *** Script: {
      "number": "INC0009004",
      "description": "While launching the defect tracking base URL, it is redirecting to an error page.",
      "sys_id": "e329de99731423002728660c4cf6a73c"
    }
    *** Script: {
      "number": "INC0009003",
      "description": "Having an issue with users trying to access the company portal app",
      "sys_id": "9fffc328731823002728660c4cf6a742"
    }
    *** Script: {
      "number": "INC0009002",
      "description": "My computer is not detecting the headphone device. It could be an issue with the USB port.",
      "sys_id": "1c832706732023002728660c4cf6a7b9"
    }

    この例は、子インシデントの合計によって集計結果を並べ替える方法を示しています。

    var aggQuery = new GlideQuery('incident')
        .aggregate('sum', 'child_incidents')
        .groupBy('category')
        .orderByDesc('sum', 'child_incidents')
        .select()
        .forEach(function (category){
            gs.info(JSON.stringify(category, null, 2));    
        });

    出力:

    *** Script: {
      "group": {
        "category": "hardware"
      },
      "sum": {
        "child_incidents": 2
      }
    }
    *** Script: {
      "group": {
        "category": "inquiry"
      },
      "sum": {
        "child_incidents": 1
      }
    }
    *** Script: {
      "group": {
        "category": "software"
      },
      "sum": {
        "child_incidents": 0
      }
    }
    *** Script: {
      "group": {
        "category": ""
      },
      "sum": {
        "child_incidents": null
      }
    }
    *** Script: {
      "group": {
        "category": "database"
      },
      "sum": {
        "child_incidents": null
      }
    }
    *** Script: {
      "group": {
        "category": "network"
      },
      "sum": {
        "child_incidents": null
      }
    }

    GlideQuery - orWhere(文字列 fieldOrQuery, 文字列 operator, 任意 value)

    指定された条件に基づいて値を返すクエリに OR 句を追加します。

    注:
    このメソッドの前に where()whereNull()、または whereNotNull() メソッドを配置します。
    表 : 39. パラメーター
    名前 タイプ 説明
    fieldOrQuery 文字列または GlideQuery where 節で使用されるフィールドまたは別の GlideQuery オブジェクト。フィールドを渡すと、目的の値にドット連結できます。例:'company.name'
    operator 文字列 オプション。OR 句で使用される演算子。引数を渡さない場合は、= 演算子が使用されます。プレースホルダー値を含める必要はありません。
    value 任意 OR 句で使用される値。
    表 : 40. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、単純な OR 句をクエリに追加する方法を示しています。

    var query = new global.GlideQuery('sys_user')
       .where('failed_attempts', '>', 0)
       .orWhere('last_login', '<', '2019-04-15')
       .select()
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "sys_id": "005d500b536073005e0addeeff7b12f4"
    }
    *** Script: {
      "sys_id": "d999e5fc77e72300454792718a10611d"
    }
    *** Script: {
      "sys_id": "30ad318577ab2300454792718a10619e"
    }
    *** Script: {
      "sys_id": "4ac73ecd738123002728660c4cf6a72c"
    }
    *** Script: {
      "sys_id": "3883f4c0730123002728660c4cf6a754"
    }
    *** Script: {
      "sys_id": "3988a3ca732023002728660c4cf6a757"
    }
    *** Script: {
      "sys_id": "8ff5b254b33213005e3de13516a8dcf7"
    }

    この例では、別のクエリを含む orWhere 句を追加する方法を示します。

    // active = true OR (title = 'Vice President' AND state = 'CA')
    var query = new GlideQuery('sys_user')
       .where('active', true)
       .orWhere(new GlideQuery()
           .where('title', 'Vice President')
           .where('state', 'CA'))
       .select('name')
       .limit(5)
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "name": "ITIL User",
      "sys_id": "681b365ec0a80164000fb0b05854a0cd"
    }
    *** Script: {
      "name": "SOAP Guest",
      "sys_id": "155699460a0a0b2b009e27c10b7f68f6"
    }
    *** Script: {
      "name": "System Administrator",
      "sys_id": "6816f79cc0a8016401c5a33be04be441"
    }
    *** Script: {
      "name": "Alva Pennigton",
      "sys_id": "ca826bf03710200044e0bfc8bcbe5d89"
    }
    *** Script: {
      "name": "Benchmark Scheduler",
      "sys_id": "d3383a875b0132003d1c484c11f91a5b"
    }

    GlideQuery - orWhereNotNull(文字列 field)

    指定されたフィールドに null 値を含まないレコードを返す OR 句を追加します。

    注:
    このメソッドの前に where()whereNull()、または whereNotNull() メソッドを配置します。
    表 : 41. パラメーター
    名前 タイプ 説明
    field 文字列 クエリで使用されるフィールド。
    表 : 42. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例では、User テーブルをクエリし、姓または名が null ではない結果を返す方法を示します。

    var query = new global.GlideQuery('sys_user')
       .whereNotNull('first_name')
       .orWhereNotNull('last_name')
       .select('name')
       .limit(5)
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "name": "ITIL User",
      "sys_id": "681b365ec0a80164000fb0b05854a0cd"
    }
    *** Script: {
      "name": "System Administrator",
      "sys_id": "6816f79cc0a8016401c5a33be04be441"
    }
    *** Script: {
      "name": "Alva Pennigton",
      "sys_id": "ca826bf03710200044e0bfc8bcbe5d89"
    }
    *** Script: {
      "name": "Benchmark Scheduler",
      "sys_id": "d3383a875b0132003d1c484c11f91a5b"
    }
    *** Script: {
      "name": "Allyson Gillispie",
      "sys_id": "06826bf03710200044e0bfc8bcbe5d8a"
    }

    GlideQuery - orWhereNull(文字列 field)

    指定されたフィールドに null 値を含むレコードを返すクエリに OR 句を追加します。

    注:
    このメソッドの前に where()whereNull()、または whereNotNull() メソッドを配置します。
    表 : 43. パラメーター
    名前 タイプ 説明
    field 文字列 クエリで使用されるフィールド。
    表 : 44. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、ユーザーテーブルをクエリし、名と姓が null であるレコードを返す方法を示しています。

    var query = new global.GlideQuery('sys_user')
       .whereNull('last_name')
       .orWhereNull('first_name')
       .select('first_name', 'last_name')
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "first_name": "ml.admin",
      "last_name": null,
      "sys_id": "88aad6c5c73003005f1b78d48b9763a5"
    }
    *** Script: {
      "first_name": "Sitemap Scheduler User",
      "last_name": null,
      "sys_id": "85388c25b71011104eed4643ae11a993"
    }
    *** Script: {
      "first_name": null,
      "last_name": "Guest",
      "sys_id": "5136503cc611227c0183e96598c4f706"
    }
    *** Script: {
      "first_name": "ml_report.user",
      "last_name": null,
      "sys_id": "b6bad6c5c73003005f1b78d48b97632a"
    }

    GlideQuery - parse(文字列 table, 文字列 encoded_query)

    エンコードされたクエリを新しい GlideQuery クエリに追加します。

    このメソッドは、すべての GlideRecord エンコードクエリ演算子をサポートしているわけではありません。現在、次の演算子がサポートされています。

    = ANYTHING GT_FIELD NOT IN
    != BETWEEN GT_OR_EQUALS_FIELD NOT LIKE
    > CONTAINS IN NSAMEAS
    >= DOES NOT CONTAIN INSTANCEOF ON
    < DYNAMIC LIKE SAMEAS
    <= EMPTYSTRING LT_FIELD STARTSWITH
    ENDSWITH LT_OR_EQUALS_FIELD
    表 : 45. パラメーター
    名前 タイプ 説明
    table 文字列 クエリするテーブル (タスクやインシデントなど)。
    encoded_query 文字列 指定されたテーブルのレコードに適用するエンコードされたクエリ
    表 : 46. 返される内容
    タイプ 説明
    GlideQuery エンコードクエリを含む GlideQuery オブジェクト。

    次の例では、タスクテーブル内のアクティブなすべてのレコードを含む GlideQuery オブジェクトを作成し、[優先度] フィールドで並べ替え、説明フィールドのみを含む Stream オブジェクトとして返します。

    GlideQuery.parse('task', 'active=true^ORDERBYpriority') // Pass the encoded query to use to parse the Task records
      .select('description') // Return the records that match the encoded query
      .forEach(doSomething); // Do some processing on each of the returned records

    GlideQuery - select(文字列 fields)

    指定されたフィールドを含む Stream オブジェクトとしてクエリの結果を返します。

    注:
    Stream クラスのターミナルメソッドを使用して、クエリの結果を取得します。詳細については、「Stream」を参照してください。

    フィールド名にフラグを追加して、フィールドの値の代わりにフィールドのメタデータを返すことができます。たとえば、フィールド名 company$DISPLAY を使用すると、会社フィールドの表示値が返されます。使用可能なフラグは次のとおりです。

    • DISPLAY:フィールドの表示値を返します。
    • CURRENCY_CODE:通貨フィールドの通貨コードを返します。例:USD
    • CURRENCY_DISPLAY:通貨フィールドの通貨表示値を返します。例:¥123.45
    • CURRENCY_STRING:通貨フィールドの通貨文字列を返します。例:JPY;123.45
    表 : 47. パラメーター
    名前 タイプ 説明
    フィールド 文字列または文字列のアレイ オプション。結果を表示するフィールド。任意の数のフィールドを引数として指定するか、目的の値にドット連結するか、フラグを使用することができます。例:
    select('first_name', 'location.city', 'company$DISPLAY');
    または
    select(['first_name', 'location.city', 'company$DISPLAY']);

    デフォルト:システムは常に sys_id を返します。

    表 : 48. 返される内容
    タイプ 説明
    ストリーム レコードなどのアイテムのストリームを操作するために使用されるオブジェクト。

    この例では、クエリから表示するフィールドを選択し、$DISPlay を使用してフィールドの表示値を返す方法を示します。

    var stream = new global.GlideQuery('sys_user')
       .select('first_name', 'last_name', 'company$DISPLAY')
       .limit(5)
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "first_name": "System",
      "last_name": "Administrator",
      "company$DISPLAY": "",
      "sys_id": "6816f79cc0a8016401c5a33be04be441"
    }
    *** Script: {
      "first_name": "Alva",
      "last_name": "Pennigton",
      "company$DISPLAY": "ACME North America",
      "sys_id": "ca826bf03710200044e0bfc8bcbe5d89"
    }
    *** Script: {
      "first_name": "Benchmark",
      "last_name": "Scheduler",
      "company$DISPLAY": "",
      "sys_id": "d3383a875b0132003d1c484c11f91a5b"
    }
    *** Script: {
      "first_name": "Allyson",
      "last_name": "Gillispie",
      "company$DISPLAY": "ACME North America",
      "sys_id": "06826bf03710200044e0bfc8bcbe5d8a"
    }
    *** Script: {
      "first_name": "SOAP",
      "last_name": "Guest",
      "company$DISPLAY": "",
      "sys_id": "155699460a0a0b2b009e27c10b7f68f6"
    }

    GlideQuery - selectOne(文字列 fields)

    指定されたフィールドを含む Optional オブジェクトとしてクエリの結果を返します。

    単一のレコードを返す場合、またはレコードが存在するかどうかをテストする場合は、このメソッドを使用します。複数のレコードを返す場合は、select() メソッドを使用して Stream オブジェクトを返します。

    フィールド名にフラグを追加して、フィールドの値の代わりにフィールドのメタデータを返すことができます。たとえば、フィールド名 company$DISPLAY を使用すると、会社フィールドの表示値が返されます。使用可能なフラグは次のとおりです。

    • DISPLAY:フィールドの表示値を返します。
    • CURRENCY_CODE:通貨フィールドの通貨コードを返します。例:USD
    • CURRENCY_DISPLAY:通貨フィールドの通貨表示値を返します。例:¥123.45
    • CURRENCY_STRING:通貨フィールドの通貨文字列を返します。例:JPY;123.45
    表 : 49. パラメーター
    名前 タイプ 説明
    fields 文字列または文字列のアレイ オプション。結果を表示するフィールド。任意の数のフィールドを引数として指定するか、目的の値にドット連結するか、フラグを使用することができます。例:
    selectOne('first_name', 'location.city', 'company$DISPLAY');
    または
    selectOne(['first_name', 'location.city', 'company$DISPLAY']);

    デフォルト:システムは常に sys_id を返します。

    表 : 50. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、単一のレコードを Optional オブジェクトとして返し、指定されたフィールドを表示する方法を示しています。

    var user = new global.GlideQuery('sys_user')
        .where('zip', '12345')
        .whereNotNull('last_name')
        .selectOne('first_name', 'last_name', 'company$DISPLAY')
        .get();
    
    gs.info(JSON.stringify(user, null, 2));

    出力:

    {
       "first_name":"Abel",
       "last_name":"Tuter",
       "company$DISPLAY":"ACME South America",
       "sys_id":"62826bf03710200044e0bfc8bcbe5df1"
    }

    GlideQuery - sum(文字列 field)

    指定された数値フィールドの集計合計を返します。

    このメソッドは、次のタイプのフィールドでのみ使用できます。
    • 整数
    • 浮動小数点数
    • 倍精度
    • 通貨
    表 : 51. パラメーター
    名前 タイプ 説明
    field 文字列 操作を実行するフィールド。
    表 : 52. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例は、cmdb_ci テーブルのすべての障害の合計を返す方法を示しています。

    var totalFaults = new global.GlideQuery('cmdb_ci')
        .sum('fault_count')
        .orElse(0);
    
    gs.info(JSON.stringify(totalFaults));

    出力:

    10

    GlideQuery - toGlideRecord()

    現在のクエリを表す GlideRecord オブジェクトを返します。クエリが GlideQuery.aggregate() メソッドを使用する場合は、GlideAggregrate オブジェクトを返します。

    クエリを変換した後、GlideRecord または GlideAggregate クラスの query() メソッドを使用してデータベースをクエリします。

    表 : 53. パラメーター
    名前 タイプ 説明
    なし
    表 : 54. 返される内容
    タイプ 説明
    GlideRecord または GlideAggregate クエリを含む GlideRecord オブジェクト。GlideQuery.aggregate() メソッドを使用した場合、メソッドは代わりに GlideAggregrate オブジェクトを返します。

    この例では、GlideQuery オブジェクトを GlideRecord に変換する方法を示します。

    var userGr = new global.GlideQuery('sys_user')
        .where('active', true)
        .whereNotNull('first_name')
        .limit(10)
        .toGlideRecord();
    userGr.query();

    GlideQuery - update(オブジェクト changes, オブジェクト selectedFields)

    定義された条件に一致する既存のレコードを更新します。

    このメソッドを使用する前に、where() メソッドを呼び出して、レコードが更新するために満たす必要がある条件を指定します。

    表 : 55. パラメーター
    名前 タイプ 説明
    変更 オブジェクト レコードで更新する名前と値のペアを含むオブジェクト。名前はテーブルのフィールドと一致する必要があります。
    selectedFields アレイ オプション。結果で返す追加フィールド。

    デフォルト:システムは常に sys_id を返します。

    表 : 56. 返される内容
    タイプ 説明
    オプション 単一のレコードを操作するために使用されるオブジェクト。

    この例では、新しい値でレコードを更新する方法を示します。

    var updateRecord = new global.GlideQuery('sys_user')
        .where('sys_id', '0a826bf03710200044e0bfc8bcbe5d7a')
        .update({ city: 'Los Angeles' });

    GlideQuery - updateMultiple(オブジェクト changes)

    定義された条件に一致する既存のすべてのレコードを更新します。更新されたレコードの数を返します。

    このメソッドを使用する前に、where() メソッドを呼び出して、レコードが更新するために満たす必要がある条件を指定します。

    表 : 57. パラメーター
    名前 タイプ 説明
    変更 オブジェクト レコードで更新する名前と値のペアを含むオブジェクト。名前はテーブルのフィールドと一致する必要があります。
    表 : 58. 返される内容
    タイプ 説明
    オブジェクト 更新されたレコードの数を含むオブジェクト。キーは次のとおりです。
    • rowCount:テーブルで更新された行の数。

    この例は、定義された基準に適合するレコードを更新する方法を示しています。

    var update = new global.GlideQuery('sys_user')
        .where('active', false)
        .where('last_name', 'Griffey')
        .updateMultiple({ active: true });
    
    gs.info(JSON.stringify(update));

    出力:

    {"rowCount":1}

    GlideQuery - where(文字列 fieldOrQuery, 文字列 operator, 任意 value)

    指定された条件に基づいて値を返すクエリに where 句を追加します。

    注:
    このメソッドの前に orWhere()orWhereNull()、または orWhereNotNull() メソッドを配置しないでください。
    表 : 59. パラメーター
    名前 タイプ 説明
    fieldOrQuery 文字列または GlideQuery where 節で使用されるフィールドまたは別の GlideQuery オブジェクト。フィールドを渡すと、目的の値にドット連結できます。例:'company.name'
    operator 文字列 オプション。where 句で使用される演算子。引数を渡さない場合は、= 演算子が使用されます。
    value 任意 where 句で使用される値。
    表 : 60. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例では、active が true で、ユーザーの最後のログインが指定された日付より後であるレコードを User テーブルから返す方法を示します。

    var query = new global.GlideQuery('sys_user')
       .where('active', true)
       .where('last_login', '>', '2018-01-01')
       .limit(5)
       .select()
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "sys_id": "6816f79cc0a8016401c5a33be04be441"
    }
    *** Script: {
      "sys_id": "005d500b536073005e0addeeff7b12f4"
    }
    *** Script: {
      "sys_id": "d999e5fc77e72300454792718a10611d"
    }
    *** Script: {
      "sys_id": "30ad318577ab2300454792718a10619e"
    }
    *** Script: {
      "sys_id": "3883f4c0730123002728660c4cf6a754"
    }

    この例では、アクティブが true で、優先度または重大度が 1 のレコードをインシデントテーブルから返す方法を示します。

    // active = true AND (priority = 1 OR severity = 1)
    var query = new GlideQuery('incident')
        .where('active', true)
        .where(new GlideQuery()
            .where('priority', 1)
            .orWhere('severity', 1))
       .limit(5)
       .select()
       .toArray(5)
    
    gs.info(JSON.stringify(query, null, 2));

    出力:

    [
       {
          "sys_id":"b0f31e5673500010c2e7660c4cf6a711"
       },
       {
          "sys_id":"8ff5b254b33213005e3de13516a8dcf7"
       },
       {
          "sys_id":"d999e5fc77e72300454792718a10611d"
       },
       {
          "sys_id":"30ad318577ab2300454792718a10619e"
       },
       {
          "sys_id":"3883f4c0730123002728660c4cf6a754"
       }
    ]

    GlideQuery - whereNotNull(文字列 field)

    指定されたフィールドに null 値を含まないレコードを返します。

    注:
    このメソッドの前に orWhere()orWhereNull()、または orWhereNotNull() メソッドを配置しないでください。
    表 : 61. パラメーター
    名前 タイプ 説明
    field 文字列 クエリで使用されるフィールド。
    表 : 62. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、ユーザーテーブルをクエリし、[first_name] フィールドが null でない結果を返す方法を示しています。

    var query = new global.GlideQuery('sys_user')
       .whereNotNull('first_name')
       .select('name')
       .limit(5)
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "name": "System Administrator",
      "sys_id": "6816f79cc0a8016401c5a33be04be441"
    }
    *** Script: {
      "name": "Alva Pennigton",
      "sys_id": "ca826bf03710200044e0bfc8bcbe5d89"
    }
    *** Script: {
      "name": "Benchmark Scheduler",
      "sys_id": "d3383a875b0132003d1c484c11f91a5b"
    }
    *** Script: {
      "name": "Allyson Gillispie",
      "sys_id": "06826bf03710200044e0bfc8bcbe5d8a"
    }
    *** Script: {
      "name": "SOAP Guest",
      "sys_id": "155699460a0a0b2b009e27c10b7f68f6"
    }

    GlideQuery - whereNull(文字列 field)

    指定されたフィールドに null 値を含むレコードを返します。

    注:
    このメソッドの前に orWhere()orWhereNull()、または orWhereNotNull() メソッドを配置しないでください。
    表 : 63. パラメーター
    名前 タイプ 説明
    field 文字列 クエリで使用されるフィールド。
    表 : 64. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、ユーザーテーブルをクエリし、名と姓が null であるレコードを返す方法を示しています。

    var query = new global.GlideQuery('sys_user')
       .whereNull('last_name')
       .orWhereNull('first_name')
       .select('first_name', 'last_name')
       .forEach(function (user){
          gs.info(JSON.stringify(user, null, 2));
       });

    出力:

    *** Script: {
      "first_name": "ml.admin",
      "last_name": null,
      "sys_id": "88aad6c5c73003005f1b78d48b9763a5"
    }
    *** Script: {
      "first_name": "Sitemap Scheduler User",
      "last_name": null,
      "sys_id": "85388c25b71011104eed4643ae11a993"
    }
    *** Script: {
      "first_name": null,
      "last_name": "Guest",
      "sys_id": "5136503cc611227c0183e96598c4f706"
    }
    *** Script: {
      "first_name": "ml_report.user",
      "last_name": null,
      "sys_id": "b6bad6c5c73003005f1b78d48b97632a"
    }

    GlideQuery - withAcls()

    GlideRecordSecure API を使用してクエリを実行し、ACL を遵守しながらデータベースを安全にクエリします。

    表 : 65. パラメーター
    名前 タイプ 説明
    なし
    表 : 66. 返される内容
    タイプ 説明
    GlideQuery 構築中のクエリオブジェクト。

    この例は、ACL を使用してセキュアなクエリを実行する方法を示しています。

    var users = new global.GlideQuery('sys_user')
        .withAcls()
        .limit(5)
        .orderByDesc('first_name')
        .select('first_name')
        .forEach(function (user){
            gs.info(JSON.stringify(user, null, 2));
        });

    出力:

    *** Script: {
      "first_name": "Zane",
      "sys_id": "16826bf03710200044e0bfc8bcbe5dbc"
    }
    *** Script: {
      "first_name": "Zackary",
      "sys_id": "8a826bf03710200044e0bfc8bcbe5d69"
    }
    *** Script: {
      "first_name": "Yvette",
      "sys_id": "4e826bf03710200044e0bfc8bcbe5d57"
    }
    *** Script: {
      "first_name": "Winnie",
      "sys_id": "f682abf03710200044e0bfc8bcbe5d1d"
    }
    *** Script: {
      "first_name": "Wilmer",
      "sys_id": "42826bf03710200044e0bfc8bcbe5d7b"
    }