简体   繁体   中英

SuiteScript 2.0 Saved Search Filters: Adding Fields not in the Transaction

I am trying to run a saved search and add a filter to it that adds fields from associated records, but not the return type of transaction. I am assuming that this will be a join but not exactly sure how this works. The search is done in a Map Reduce script and it is supposed to do the following:

Find items in transactions with the same name and different class type. The Saved search looks for Sales Orders that are still open and the item name and class are the variables. I have that information readily available in the getInputData function but am running into problems adding the filter. How should that look? I have this as the code:

var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
mySearch.filters.push(search.createFilter({ name: 'itemid', join: 'item', operator: 'IS', values: [itemName] }));
mySearch.filters.push(search.createFilter({ name: 'class', join: 'item', operator: 'ISNOT', values: [itemClass] }));

I did have the filter look like this:

var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
mySearch.filters.push(search.createFilter({ name: 'itemid', operator: 'IS', values: [itemName] }));
mySearch.filters.push(search.createFilter({ name: 'class', operator: 'ISNOT', values: [itemClass] }));

Can anyone point to exactly why this is not working? I take out the filter and the search runs perfectly. I add the filter and getInputData chokes on the search.

Thank you for your time!

For sure, if this is "transaction" search, the filters should be "join". Try to change the first filter field from "itemid" to "name". Like this:

var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
mySearch.filters.push(search.createFilter({ name: 'name', join: 'item', operator: 'IS', values: [itemName] }));
mySearch.filters.push(search.createFilter({ name: 'class', join: 'item', operator: 'ISNOT', values: [itemClass] }));

I know that it sounds stupid, but give it a try.
And one different approach - You can use filterExpression, instead of filters. Like this:

var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
var myAdditionalFilters = [ 'and', ['item.name', 'is', [itemName] ],
                            'and', ['item.class', 'isnot', [itemClass] ]
                          ];
var myNewFilterExpression = mySearch.filterExpression.concat(myAdditionalFilters);
mySearch.filterExpression = myNewFilterExpression;

As it turned out, I was confused on how the saved search gathered information and applying the filter the way I was created basically an endless loop. The idea is to search the Sales Orders after a User change an Item's class and update the Sales Order's line items to the new class should the Line Item be the same Item Name and a different Class. So the filter wound up looking like this instead:

 var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
 var filters = mySearch.filters;
 filters.push(search.createFilter({ name: 'name', join: 'item', operator: 'IS', values: [itemName] }));
 filters.push(search.createFilter({ name: 'class', operator: 'NONEOF', values: [itemClass] }));
 mySearch.filters = filters;

With that, the search worked. I now have a new problem described in one of my replies below. The searchResults in the map function look like this:

{"recordType":null,"id":"1","values":{"GROUP(tranid)":"289092"}}

I can't seem to get the value from that key value pair. I use "entity" and it's undefined. I use "tranid" and it's undefined. I've tried several different combinations and I can't get to that value.

Since I answered this question with the above filters, this is a new question indeed. But if someone could throw some light on this, I would be extremely grateful.

It is better to ask your second question separate. Anyway, in your case, because of the grouping function in the search, 'tranid' in map stage should be accessed like this:

        var searchResult = JSON.parse(context.value);
        var salesOrderId = searchResult.id;
        var tranId = searchResult.values['GROUP(tranid)'];

I'm not sure if this is your problem, but I have found that in some cases where you load an existing search you cannot "push" directly to the search filters object, and need to assign it to a temporary variable first. Maybe something weird on the Rhino backend.

var mySearch = search.load({ id: 'customsearch_so_itemclassid' });
var filters = mySearch.filters;
filters.push(search.createFilter({ name: 'itemid', operator: 'IS', values: [itemName] }));
filters.push(search.createFilter({ name: 'class', operator: 'ISNOT', values: [itemClass] }));
mySearch.filters = filters;

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