简体   繁体   English

NetSuite中的脚本搜索无法正常工作

[英]Scripting search in NetSuite not working as expected

I tried a search script in NetSuite and the problem I'm having is that the search is getting several results despite I put the order number in the transaction table search. 我在NetSuite中尝试了一个搜索脚本,但我遇到的问题是,尽管我将订单号放在了交易表搜索中,但搜索却得到了多个结果。 If I do the same search by the IU I just get 1 result which is the correct one. 如果我通过IU进行相同的搜索,我只会得到1个结果,这是正确的结果。

The script is 该脚本是

var filters = new Array();

filters[0] = new nlobjSearchFilter('item', null, 'is', 'ITEM123');
filters[1] = new nlobjSearchFilter('type', null, 'is', 'SalesOrd');
filters[2] = new nlobjSearchFilter('companyname', 'customer', 'contains', 'CustomerName');
filters[3] = new nlobjSearchFilter('number', null, 'is', 'ORDER9887');


var columns = new Array();
columns[0] = new nlobjSearchColumn('item');
columns[1] = new nlobjSearchColumn('type');
columns[2] = new nlobjSearchColumn('name','item');
columns[3] = new nlobjSearchColumn('companyname','customer');
columns[4] = new nlobjSearchColumn('number');


var searchResults = nlapiSearchRecord('transaction', null, filters, columns);
var values = 'TOTAL RESULTS: ' + searchResults.length;
if(searchResults != null)
{
  for( i = 0 ; i<  searchResults.length ; i++)
  {
   values = values + '\r\nITEM ' +  searchResults[i].getValue(columns[0]) + 
            '\r\nTYPE ' +  searchResults[i].getValue(columns[1]) +
            '\r\nITEM NAME ' + searchResults[i].getValue(columns[2]) +
            '\r\nCOMPANY NAME ' + searchResults[i].getValue(columns[3]) +
            '\r\nTRANSACTION NUMBER ' + searchResults[i].getValue(columns[4]); 
  }
  alert(values);
}

So it doesn't make much sense to me, it's suppose that filters are implicitly with an AND operator. 所以这对我来说没有多大意义,它假定过滤器是隐式地使用AND运算符。

Is there any clue which I'm doing wrong? 有什么线索我做错了吗?

Thanks in advance. 提前致谢。

Pablo. 巴勃罗。

When searching transactions, you need to understand and use the mainline filter to adjust the results you want. 搜索交易时,您需要了解并使用mainline过滤器来调整所需的结果。 See this answer for a more detailed explanation. 有关更多详细说明,请参见此答案

Unfortunately, I cannot explain why you are getting different results for the exact same criteria between the UI and the script. 不幸的是,我无法解释为什么在UI和脚本之间完全相同的条件下会得到不同的结果。 I would need to see the UI search as well to troubleshoot that. 我还需要查看UI搜索来解决该问题。

You are correct that search filters provided this way are always with an AND operator. 您是正确的,以这种方式提供的搜索过滤器始终使用AND运算符。 You can instead use filter expressions to explicitly state the logical operator. 您可以改用过滤器表达式来明确声明逻辑运算符。 See the NetSuite Help document titled Filtering a Search for examples of filter expressions. 有关过滤器表达式的示例,请参阅标题为“ 过滤搜索 ”的NetSuite帮助文档。

Using filter expressions and the mainline filter, I might write your search something like: 使用过滤器表达式和主线过滤器,我可以将您的搜索编写为:

// Filter expression syntax
var filters = [
    ['item', 'is', 'ITEM123'], 'and',
    ['type', 'is', 'SalesOrd'], 'and',
    ['mainline', 'is', 'F'], 'and', // mainline=F gives me only line item results
    ['customer.companyname', 'contains', 'CustomerName'], 'and',
    ['number', 'is', 'ORDER9887']
];

var columns = [
    new nlobjSearchColumn('item'),
    new nlobjSearchColumn('type'),
    new nlobjSearchColumn('name','item'),
    new nlobjSearchColumn('companyname','customer'),
    new nlobjSearchColumn('number')
];

// I always default my search results to [] to avoid the null check later
var searchResults = (nlapiSearchRecord('transaction', null, filters, columns) || []);

// Logging to console assuming client script, otherwise use nlapiLogExecution
console.log('TOTAL RESULTS: ' + searchResults.length);

// I prefer using Array.map or Array.forEach when iterating over arrays in SuiteScript
var resultString = searchResults.map(
    function (result) { // Change each result to its String representation
        return 'ITEM ' + result.getValue('item') + 
            '\r\nTYPE ' +  result.getValue('type') +
            '\r\nITEM NAME ' + result.getValue('name', 'item') +
            '\r\nCOMPANY NAME ' + result.getValue('companyname','customer') +
            '\r\nTRANSACTION NUMBER ' + result.getValue('number'); 
    }
).join("\r\n"); // Join all of the result strings with a newline

console.log(resultString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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