简体   繁体   中英

How to apply multiple filters in savedsearch through Suitescript 2.0?

I am Applying filters in my existing saved search, but it's not getting applied. Can anyone help me what's wrong in this, as I'm new in Suitescripts?

Filters: gender and item are being passed from Suitelet to this Scheduled Script in a parameter:

var slfilters = runtime.getCurrentScript().getParameter({ name: 'custscript_searchfilter_report' });
  //
  // ─── MAIN FUNCTION───────────────────────────────────────────────────
  //
  function generateReport() {
    var slfilters = runtime.getCurrentScript().getParameter({
      name: 'custscript_searchfilter_report'
    });
    log.debug('slfilters', slfilters);

    if (!!slfilters) {
      slfilters = JSON.parse(slfilters);
    }
    log.debug('slfilters2', slfilters);

    //var getUser = runtime.getCurrentUser();

    var gender = slfilters.isgender
    log.debug('gender', gender)
    var item = slfilters.isItem
    log.debug('item', item)

    var item = getItems(item, gender);
    log.debug('items table', item)
    // return item;

    var xmlTemplateFile = file.load(3918);
    //var template = script.getParameter({ name: 'custscript_template' });
    var renderer = render.create();
    renderer.templateContent = xmlTemplateFile.getContents();
    var customSources = {
      alias: 'searchdata',
      format: render.DataSource.JSON,
      data: JSON.stringify({
        value: item,
      })
    };
    renderer.addCustomDataSource(customSources);

    var xml = renderer.renderAsString();
    var pdf = render.xmlToPdf({
      "xmlString": xml
    });

    email.send({
      author: 317,
      recipients: 'aniswtf@gmail.com',
      subject: 'Item Report',
      body: 'Report Generated: ',
      attachments: [pdf]
    });
  }

  //
  // ─── SEARCH ───────────────────────────────────────────────────
  //
  function getItems(item, gender) {
    try {
      var itemSearch = search.load({
        id: 'customsearch_mx_itemsearch'
      });
      log.error('itemSearch', itemSearch)

      var defaultFilters = itemSearch.filters;
      var arrFilters = [];

      arrFilters.push(search.createFilter({
        name: 'custitem5', //gender
        operator: 'anyof',
        values: gender
      }));

      arrFilters.push(search.createFilter({
        name: 'itemid',
        operator: 'anyof',
        values: item
      }));

      defaultFilters = defaultFilters.concat(arrFilters);
      log.error('Updated Filters', defaultFilters)

      var results = itemSearch.run().getRange({
        start: 0,
        end: 150
      });

      results.map(function(x) {
        return {
          'category': x.getText({
            name: "custitem10",
            join: "parent"
          }),
          'season': x.getValue({
            name: "custitem11",
            join: "parent"
          }),
          'riselabel': x.getValue({
            name: "custitem_itemriselabel",
            join: "parent"
          }),
          'fit': x.getValue({
            name: "custitem9",
            join: "parent"
          }),
          'name': x.getValue({ //sku
            name: "itemid",
            join: "parent"
          }),
          'style': x.getValue({
            name: "custitem8",
            join: "parent"
          }),
          'inseam': x.getValue({
            name: "custitem7",
            join: "parent"
          }),
          'wash': x.getValue({
            name: "custitem_washname",
            join: "parent"
          }),
        };
      });
      return results;
    } catch (e) {
      log.error('error in getItems', e)
    }
  }
  return {
    execute: execute
  };
});

In SuiteScript 1.0 there was a nlobjSearch method "setFilters". SuiteScript 2.0 does not seem to have an equivalent but you still need to somehow set the filters. It looks like that is the missing step. Perhaps try to push the new filters onto/in place of the current search filters with

itemSearch.filters.push(defaultFilters);

not sure if it'd be desired to then save the search or not. I hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM