您需要将每个句点指定为单独的过滤器,并使用.setParens(1) 和.setOr(true) 来构建您的搜索逻辑,如下所示:
var results = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('mainline', null, 'is', 'T'),
new nlobjSearchFilter('postingperiod', null, 'within', 122).setLeftParens(1).setOr(true),
new nlobjSearchFilter('postingperiod', null, 'within', 123).setRightParens(1)
], [
new nlobjSearchColumn('internalid', null, 'count')
]);
如果您并不总是知道需要哪些时段,您可以使用如下函数动态生成这些过滤器:
function buildPeriodFilters(periodIds) {
// Return empty array if nothing is passed in so our search doesn't break
if (!periodIds) {
return [];
}
// convert to array if only a single period id is passed in.
periodIds = [].concat(periodIds);
return periodIds.map(function(periodId, index, periodIds) {
var filter = new nlobjSearchFilter('postingperiod', null, 'within', periodId);
// if this is the first periodid, add a left parenthesis
if (index === 0) {
filter = filter.setLeftParens(1);
}
// if this is the last period id, add a right parenthesis, otherwise add an 'or' condition
if (index !== periodIds.length - 1) {
filter = filter.setOr(true);
} else {
filter = filter.setRightParens(1);
}
return filter;
});
}
var dynamicPeriodFilter = buildPeriodFilters([122,123,124]);
var results = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('mainline', null, 'is', 'T'),
].concat(dynamicPeriodFilter), [
new nlobjSearchColumn('internalid', null, 'count')
]);