1

In netsuite we have months (Not in serial order). months values are say :

list_month = [182,183,186,187,188,190,191,192,194,195,196,199,200,201];

I want to apply search filter to get records within certain months say from 183 to 194.

var period = 183;
var period1 = 194;

I used the "between" and "within" but it did not work'

Here is my filter:

filters.push(new nlobjSearchFilter("postingperiod","transaction","within",period,period1));

this returns only the values of 183. I want all of them : 183,186,187,188,190,191,192,194.

*Note this are not dates but months (starting from 1 to last date of that month)

How can i get this.

Thanks

1 Answers1

3

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')
]);
Mike Robbins
  • 3,184
  • 15
  • 20
  • This works but only for the to and from month. It skips the middle months, which I want. Array for the month is : list_month = [182,183,186,187,188,190,191,192,194,195,196,199,200,201]; Say I want it to be in 183 and 194. So the filter should be applied to 183,186,187,188,190,191,192,194 currently its getting applied only to 183 and 19 – Rahul Nagare Dec 08 '16 at 10:25
  • *and 194 This is how i am doing: `var period = 183; var period1 = 194; filters.push(new nlobjSearchFilter("postingperiod","transaction","between",period).setLeftParens(1).setOr(true)); filters.push(new nlobjSearchFilter("postingperiod","transaction","between",period1).setRightParens(1));` I tried : `filters.push(new nlobjSearchFilter("postingperiod","transaction","between",list_month[period]).setLeftParens(1).setOr(true)); filters.push(new nlobjSearchFilter("postingperiod","transaction","between",list_month[period1]).setRightParens(1));` but this is not working. – Rahul Nagare Dec 08 '16 at 10:33
  • Try changing 'between' to 'within' in the filter definitions. – Mike Robbins Dec 08 '16 at 17:11