NetSuite 中的脚本搜索未按预期工作
Scripting search in NetSuite not working as expected
我在 NetSuite 中尝试了一个搜索脚本,但我遇到的问题是,尽管我在交易 table 搜索中输入了订单号,但搜索得到了几个结果。如果我通过 IU 进行相同的搜索,我只会得到 1 个正确的结果。
脚本是
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);
}
所以这对我来说没有多大意义,假设过滤器隐含地带有 AND 运算符。
有什么线索是我做错了吗?
提前致谢。
巴勃罗。
搜索交易时,您需要了解并使用mainline
过滤器来调整您想要的结果。有关更详细的说明,请参阅 this answer。
不幸的是,我无法解释为什么您在 UI 和脚本之间针对完全相同的条件得到不同的结果。我还需要查看 UI 搜索以解决该问题。
您说得对,以这种方式提供的搜索过滤器始终带有 AND
运算符。您可以改用过滤器表达式来明确说明逻辑运算符。有关过滤器表达式的示例,请参阅标题为 过滤搜索 的 NetSuite 帮助文档。
使用过滤器表达式和主线过滤器,我可能会像这样编写您的搜索:
// 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);
我在 NetSuite 中尝试了一个搜索脚本,但我遇到的问题是,尽管我在交易 table 搜索中输入了订单号,但搜索得到了几个结果。如果我通过 IU 进行相同的搜索,我只会得到 1 个正确的结果。
脚本是
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);
}
所以这对我来说没有多大意义,假设过滤器隐含地带有 AND 运算符。
有什么线索是我做错了吗?
提前致谢。
巴勃罗。
搜索交易时,您需要了解并使用mainline
过滤器来调整您想要的结果。有关更详细的说明,请参阅 this answer。
不幸的是,我无法解释为什么您在 UI 和脚本之间针对完全相同的条件得到不同的结果。我还需要查看 UI 搜索以解决该问题。
您说得对,以这种方式提供的搜索过滤器始终带有 AND
运算符。您可以改用过滤器表达式来明确说明逻辑运算符。有关过滤器表达式的示例,请参阅标题为 过滤搜索 的 NetSuite 帮助文档。
使用过滤器表达式和主线过滤器,我可能会像这样编写您的搜索:
// 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);