Search code examples
javascriptnetsuitesuitescript

Restrict filter if blank in Netsuite


EDIT - Not sure I articulated myself that well so wanted to sum up:

I'm using variables as the values for a saved search filter, however those variables won't necessarily always have values, in those instances I want that filter to be disregarded.

Thank you

Thanks to a great post by BK Knights I am using a client script to pass values from a suitelet form to another suitelet

Client script snippet:

var output = url.resolveScript({
      scriptId: 'customscript1691',
      deploymentId: 'customdeploy1',
      params : {
                    customscript1691_theItem : itemId, 
                    customscript1691_manufacturer : manufacturer,
                    customscript1691_category : category,
                }

Receiving suitelet snippet

var passedItem = request.parameters.customscript1691_theItem;
var passedManufacturer = request.parameters.customscript1691_manufacturer;
var passedCategory = request.parameters.customscript1691_category;

This is all working fine and I can see the values in the logs. The problem I am having is that I need to use these values as filters for another saved search I am loading on the receiving suitelet.

No matter what configuration of filters I try it never works.

If there is no item in the item field but I have selected a manufacturer then the search returns blank because it is looking for a blank item.

How do I configure it so that if there is nothing in the field that is passed through then the filter is void and it just uses the filters with content in?

Bit of code I have tried below:

               search.createFilter({
                    name:'name',
                    operator: search.Operator.ANYOF,
                    values:[checkedpassedItem]
                }),
                search.createFilter({
                    name:'custitem_pro_manf',
                    operator: search.Operator.IS,
                    values:[checkedpassedManufacturer]
                }),
                
                search.createFilter({
                    name:'inventorylocation',
                    operator: search.Operator.IS,
                    values:[14]
                }),

I've trying performing an IF check on the passed fields to just input nothing if they are blank but this also returns nothing.

Any help greatly appreciated.

Thank you


Solution

  • Create your search first, without the filters that match the parameters you're passing.

    var theSearch = search.create({
        type: {searchType},
        columns: [
            {columm1},
            {column2},
            {etc}
        ]
    });
    

    Then create filters for the parameters conditionally, based on the value, or lack thereof in the corresponding parameter.

    var itemFilter;
    if (passedItem) {
        itemFilter = search.createFilter({
            name:'name',
            operator: search.Operator.ANYOF,
            values:[checkedpassedItem]
        });
    } else {
        itemFilter = search.createFilter({
            name:'name',
            operator: search.Operator.ANY
        });
    }
    

    Then add the filter to your search.

    theSearch.filters.push(itemFilter);
    

    You could also take another approach and only create and add the filter if there is a value. (The effect of having a filter with the ANY operator and not having the filter should be the same, however you may be using a search which has a 'default' filter included in the initial search definition, or an existing search loaded from your account, in which case you'll want to consider whether to override it within your script).

    if (passedItem) {
        theSearch.filters.push(
            search.createFilter({
                name:'name',
                operator: search.Operator.ANYOF,
                values:[checkedpassedItem]
            })
        );
    }