サーチ…


前書き

Suitescriptで検索を作成すると、Filterオブジェクトの配列またはフィルタ式のいずれかを「フィルタ」として提供できます。 2番目のオプションは読みやすく、デフォルトの "AND"だけでなく "OR"および "NOT"演算子を使用してネストされた式(最大3レベル)を提供する非常に柔軟なオプションを提供します。

フィルター用語

フィルタの式を理解するには、Filter Termから始めます。これは、少なくとも3つの要素を含む単純な文字列の配列です

  1. フィルタ (フィールド/結合フィールド/式/サマリー)
  2. 演算子 (search.Operator)
  3. (文字列値(または文字列値の配列)、フィルタパラメータとして使用される)
 // Simple example:
 ['amount', 'equalto', '0.00']
 
 // When the field is checkbox, use 'T' or 'F'
 ['mainline', 'is', 'T']

 // You can use join fields
 ['customer.companyname', 'contains', 'ltd']

 // summary filter term
 ['sum(amount)', 'notlessthan', '170.50']

 // summary of joined fields
 ['sum(transaction.amount)', 'greatherthan', '1000.00']

 // formula:
 ["formulatext: NVL({fullname},'John')", "contains", "ohn"]

 // and even summary formula refering joined fields:
 ['sum(formulanumeric: {transaction.netamount} + {transaction.taxtotal})', 'greaterthanorequalto','100.00']

 // for selection fields, you may use 'anyof'
 // and put values in array
 ['type','anyof',['CustInvc','VendBill','VendCred']]

 // when using unary operator, like isempty/isnotempty
 // don't forget that the filter term array contains at least 3 elements
 // and put an empty string as third:
 ['email', 'isnotempty', '']

 // you may have more than 3 elements in Filter Term array,
 // when the operator requires more than one value:
 ['grossamount','between','100.00','200.00']

一部のセレクタフィールドでは、特別な値を使用できます。

 // When filtering the user related fields, you can use:
 // Me (Current user): @CURRENT@
 // My Team (somebody from the team I am leading): @HIERARCHY@
 ['nextapprover','is','@CURRENT@']

 // The same is valid for Subsidiary, Department, Location, etc.
 // @CURRENT@ means MINE (Subsidiary, Department...)
 // @HIERARCHY@ means MINE or DESCENDANTS
 ["subsidiary","is","@HIERARCHY@"]

 // on selection fields you may use @ANY@ and @NONE@
 ['nextapprover','is','@NONE@']

フィルタ式

単純なフィルター式も配列です。これには、オペレータ - 'AND'、 'OR'、 'NOT'と組み合わされた1つ以上のフィルタ用語が含まれています。 (演算子は大文字と小文字を区別しません)。

[ 
  ['mainline', 'is', 'T'],
  'and', ['type','anyof',['CustInvc','CustCred']],
  'and', 'not', ['amount', 'equalto', '0.00'],
  'or', ['customer.companyname', 'contains', 'ltd']
]

より複雑なフィルタ式は、フィルタ項ネストされたフィルタ式を演算子と組み合わせて含むことができます。ネストされた式のレベルは3つまでです。

[ 
  ['mainline', 'is', 'T'],
  'and', ['type','anyof',['CustInvc','CustCred']],
  'and', [ ['customer.companyname', 'contains', 'ltd'],
           'or', ['customer.companyname', 'contains', 'inc']
         ],
  'and', [ ['subsidiary', 'is', 'HQ'],
           'or', ['subsidiary', 'anyof', '@HIERARCHY@']
         ],
  'and', ['trandate', 'notbefore', 'yesterday']
 ]

そして最後に、SS2.0のサンプルにすべてまとめてみましょう。

var s = search.create({
    type    : 'transaction',        
    columns : [
               'trandate', 
               'tranid',
               'currency',
               'customer.companyname',
               'customer.country', 
               'amount' 
              ],
    filters : [ 
                ['mainline', 'is', 'T'],
                'and', ['type','anyof',['VendBill','VendCred']],
                'and', [ ['customer.companyname', 'contains', 'ltd'],
                         'or', ['customer.companyname', 'contains', 'inc']
                       ],
                'and', [ ['subsidiary', 'is', 'HQ'],
                         'or', ['subsidiary', 'anyof', '@HIERARCHY@']
                       ],
                'and', ['trandate', 'notbefore', 'yesterday']
              ]
});

フィルタ式とフィルタオブジェクト

フィルタ式にフィルタオブジェクトを含めることはできません 。これはとても重要です。フィルタ式でフィルタを作成する場合は、配列の配列を使用します。次の構文は間違っています:

// WRONG!!!
var f1 = search.createFilter({
            name: 'mainline',
            operator: search.Operator.IS,
            values: 'T'
});

var f2 = search.createFilter({
            name: 'type',
            operator: search.Operator.ANYOF,
            values: ['VendBill','VendCred']
});

// here you will receive an error message
var s = search.create({
    type    : 'transaction',        
    filters : [ f1, 'and', f2 ] // f1,f2 are Filter Objects, instead of string arrays
});

代わりに、 正しいを使用してください:

// CORRECT!!!
var f1 = ['mainline', search.Operator.IS, 'T'];

var f2 = ['type', search.Operator.ANYOF, ['VendBill','VendCred'] ];

var s = search.create({
    type    : 'transaction',        
    filters : [ f1, 'and', f2 ]
});

フィルタオブジェクトのアプローチを維持したい場合は、フィルタオブジェクトの配列を渡し、演算子 'AND'、 'OR'、 'NOT'を忘れてしまいます。それは常にANDとなるでしょう

// correct, but not useful
var f1 = search.createFilter({
            name: 'mainline',
            operator: search.Operator.IS,
            values: 'T'
});

var f2 = search.createFilter({
            name: 'type',
            operator: search.Operator.ANYOF,
            values: ['VendBill','VendCred']
});


var s = search.create({
    type    : 'transaction',        
    filters : [ f1, f2 ] // here you have array of Filter Objects,
                         // filtering only when all of them are TRUE
});

役に立つヒント

  1. ここでは、日付フィールドの検索フィルタ値のリストを見つけることができます:
    https://system.netsuite.com/app/help/helpcenter.nl?fid=section_N3010842.html
    これらは次のような式で使用できます。
['trandate', 'notbefore', 'daysAgo17']
  1. 検索演算子は次のとおりです。
    https://system.netsuite.com/app/help/helpcenter.nl?fid=section_N3005172.html
    もちろん、 serach.Operator enumを使うことができます:
    https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4345782273.html

  2. 検索サマリーの種類は次のとおりです。
    https://system.netsuite.com/app/help/helpcenter.nl?fid=section_N3010474.html

  3. ANYOF演算子は、選択されたタイプのフィールド(リスト/レコード)でのみ使用できます。フリーテキストのフィールド(名前、電子メールなど)に対して使用する場合は、唯一の方法は 'OR'演算子を使用してネストされたフィルタ式を作成することです。

[ ['email', 'startswith', '[email protected]'],
  'or', ['email', 'startswith', '[email protected]'], 
  'or', ['email', 'startswith', '[email protected]'], 
  'or', ['email', 'startswith', '[email protected]'] 
]

またはあなたの代わりにこれを行う小さなスクリプトを書くことができます:

function stringFieldAnyOf(fieldId, listOfValues) {    
    var result = [];
    if (listOfValues.length > 0) {
        for (var i = 0; i < listOfValues.length; i++) {
            result.push([fieldId, 'startswith', listOfValues[i]]);
            result.push('or');
        }
        result.pop(); // remove the last 'or'
    }
    return result;
}

// usage: (two more filters added just to illustrate how to combine with other filters)
var custSearch = search.create({
  type: record.Type.CUSTOMER,
  columns: searchColumn,
  filters: [
            ['companyname', 'startswith', 'A'], 
            'and', stringFieldAnyOf('email', ['[email protected]', '[email protected]']),
            'and', ['companyname', 'contains', 'b']
           ]

});
  1. まだ自信がない?カンニングをお探しですか? :)
    Netsuite UIで保存された検索を作成し、検索ID(customsearch1234と言うことができます)とフィルタ式のlog.debugを取ってください:
    var s = search.load('customsearch1234');
    
    log.debug('filterExpression', JSON.stringify(s.filterExpression));


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow