You would need to specify each period as a separate filter and use .setParens(1)
and .setOr(true)
to build out the logic of your search like this:
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')
]);
If you don't always know which periods you'll need, you can generate these filters dynamically with a function like this:
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')
]);