SuiteTalk 高级搜索示例
Posted
技术标签:
【中文标题】SuiteTalk 高级搜索示例【英文标题】:SuiteTalk Advanced Search Examples 【发布时间】:2018-05-12 02:28:37 【问题描述】:我只使用 NetSuite 和 SuiteTalk 不到一年。我找到了利用已保存搜索的基本搜索和高级搜索的示例,但我很难找到如何使用结果集中的条件和选定列从头开始执行高级搜索的示例。所以我在问例子。
【问题讨论】:
【参考方案1】:我查看了可从 NetSuite 下载的 NSClientERP 项目中提供的示例。这是一个简化的例子。
service.searchPreferences = new SearchPreferences();
service.searchPreferences.bodyFieldsOnly = true;
service.searchPreferences.returnSearchColumns = true;
TransactionSearchAdvanced customSearch = new TransactionSearchAdvanced()
columns = new TransactionSearchRow()
basic = new TransactionSearchRowBasic()
dateCreated = new SearchColumnDateField[] new SearchColumnDateField()
, tranDate = new SearchColumnDateField[] new SearchColumnDateField()
, type = new SearchColumnEnumSelectField[] new SearchColumnEnumSelectField()
, tranId = new SearchColumnStringField[] new SearchColumnStringField()
, internalId = new SearchColumnSelectField[] new SearchColumnSelectField()
, entity = new SearchColumnSelectField[] new SearchColumnSelectField()
, item = new SearchColumnSelectField[] new SearchColumnSelectField()
, lastModifiedDate = new SearchColumnDateField[] new SearchColumnDateField()
, itemJoin = new ItemSearchRowBasic()
itemId = new SearchColumnStringField[] new SearchColumnStringField()
,
criteria = new TransactionSearch()
basic = new TransactionSearchBasic()
type = new SearchEnumMultiSelectField()
@operator = SearchEnumMultiSelectFieldOperator.anyOf
, operatorSpecified = true
, searchValue = new string[]
"_salesOrder"
, lastModifiedDate = new SearchDateField()
@operator = SearchDateFieldOperator.onOrAfter
, operatorSpecified = true
, searchValue = DateTime.Now.AddDays(-3)
, searchValueSpecified = true
;
Console.WriteLine("Querying NetSuite");
SearchResult searchResult = service.search(customSearch);
Console.WriteLine("Query Results: " + searchResult.totalRecords.ToString());
int total = searchResult.totalRecords;
int updated = 0;
bool searchMore = false;
Console.WriteLine("\nThe search() operation for transaction was run successfully.");
Console.WriteLine("\n Total Records = " + searchResult.totalRecords);
Console.WriteLine(" Total Pages = " + searchResult.totalPages);
Console.WriteLine(" Page Size = " + searchResult.pageSize);
Console.WriteLine(" Current Page Index = " + searchResult.pageIndex);
Console.WriteLine("\n\nHit Enter to list results");
Console.ReadLine();
SearchRow[] records = searchResult.searchRowList;
if (records != null)
for (int i = 0, j = (searchResult.pageIndex - 1) * searchResult.pageSize; i < records.Length; i++, j++)
TransactionSearchRow transactionRow = (TransactionSearchRow) records[i];
TransactionSearchRowBasic transactionRowBasic = transactionRow.basic;
ItemSearchRowBasic itemRowBasic = transactionRow.itemJoin;
Console.WriteLine(
"\n Transaction Return Columns Row[" + j + "]: " +
"\n internalId=" + transactionRowBasic.internalId[0].searchValue.internalId +
"\n tranId=" + transactionRowBasic.tranId[0].searchValue +
"\n type=" + transactionRowBasic.type[0].searchValue +
(transactionRowBasic.entity == null ? "" : ("\n customer internalID=" + transactionRowBasic.entity[0].searchValue.internalId)) +
(transactionRowBasic.item == null ? "" : ("\n item=" + transactionRowBasic.item[0].searchValue.internalId)) +
(itemRowBasic == null ? "" : ("\n item.name=" + itemRowBasic.itemId[0].searchValue)) +
(transactionRowBasic.dateCreated == null ? "" : ("\n createdDate=" + transactionRowBasic.dateCreated[0].searchValue.ToString()) +
(transactionRowBasic.tranDate == null ? "" : ("\n tranDate=" + transactionRowBasic.tranDate[0].searchValue.ToString())) +
(transactionRowBasic.lastModifiedDate == null ? "" : ("\n lastModifiedDate=" + transactionRowBasic.lastModifiedDate[0].searchValue.ToString()))));
在执行高级搜索之前设置搜索首选项很重要。如果没有将 returnSearchFields 设置为 true,我没有得到任何结果。此外,您必须同时提供条件和列。在 NSClientERP 示例中,以 TransactionSearchAdvanced 开头的每个对象都在单独的代码行中被实例化、配置并链接到子对象。我发现这很难理解。我正在使用一行代码来实例化整个搜索对象。
要注意的另一件事是 itemJoin,它将 Items 表连接到销售订单行项目。您还可以使用其他联接,例如 billingTransactionJoin。利用这些连接,您可以访问相关表格以获取商品的显示名称、库存信息或商品发货时的跟踪号等信息。
除了填充数据集并在事后进行排序之外,我一直无法找到对结果进行排序的方法。如果有人有更好的方法,请告诉我。
【讨论】:
netsuite soap api支持'AND'条件,但它支持'OR'条件吗? 如果我需要做一个“或”条件,我通常使用过滤表达式。【参考方案2】:这是一个实际正确执行分页的示例。
service.searchPreferences = new SearchPreferences();
service.searchPreferences.bodyFieldsOnly = true;
service.searchPreferences.returnSearchColumns = true;
TransactionSearchAdvanced customSearch = new TransactionSearchAdvanced()
columns = new TransactionSearchRow()
basic = new TransactionSearchRowBasic()
dateCreated = new SearchColumnDateField[] new SearchColumnDateField()
,
tranDate = new SearchColumnDateField[] new SearchColumnDateField()
,
type = new SearchColumnEnumSelectField[] new SearchColumnEnumSelectField()
,
tranId = new SearchColumnStringField[] new SearchColumnStringField()
,
internalId = new SearchColumnSelectField[] new SearchColumnSelectField()
,
entity = new SearchColumnSelectField[] new SearchColumnSelectField()
,
item = new SearchColumnSelectField[] new SearchColumnSelectField()
,
lastModifiedDate = new SearchColumnDateField[] new SearchColumnDateField()
,
itemJoin = new ItemSearchRowBasic()
itemId = new SearchColumnStringField[] new SearchColumnStringField()
,
criteria = new TransactionSearch()
basic = new TransactionSearchBasic()
type = new SearchEnumMultiSelectField()
@operator = SearchEnumMultiSelectFieldOperator.anyOf
,
operatorSpecified = true
,
searchValue = new string[]
"_salesOrder"
,
lastModifiedDate = new SearchDateField()
@operator = SearchDateFieldOperator.onOrAfter
,
operatorSpecified = true
,
searchValue = DateTime.Now.AddDays(-15)
,
searchValueSpecified = true
;
Console.WriteLine("Querying NetSuite");
SearchResult res = service.search(customSearch);
Console.WriteLine("\nThe advanced search completed.");
Console.WriteLine("\n Total Records = " + res.totalRecords);
Console.WriteLine(" Total Pages = " + res.totalPages);
Console.WriteLine(" Page Size = " + res.pageSize);
Console.WriteLine(" Current Page Index = " + res.pageIndex);
Console.WriteLine("\n\nHit Enter to list results");
Console.ReadLine();
while (res.searchRowList.Length > 0)
Console.WriteLine($"\n\nProcessing page: res.pageIndex");
Console.WriteLine($"Result count: res.searchRowList.Length");
Console.WriteLine($"\nHit enter to list page res.pageIndex results");
Console.ReadLine();
foreach (TransactionSearchRow transactionRow in res.searchRowList)
TransactionSearchRowBasic transactionRowBasic = transactionRow.basic;
ItemSearchRowBasic itemRowBasic = transactionRow.itemJoin;
Console.WriteLine(
"\n tranId=" + transactionRowBasic.tranId[0].searchValue +
(itemRowBasic == null ? "" : ("\n item.name=" + itemRowBasic.itemId[0].searchValue))
);
Console.WriteLine("\nQuerying NetSuite again...");
res = service.searchMore(++res.pageIndex);
service.logout();
Console.WriteLine("\n\nHit Enter to close this window.");
Console.ReadLine();
【讨论】:
【参考方案3】:一年后我又回到了这段代码。我正在编写另一个高级搜索,并认为可能没有必要在数组定义中添加一个空白元素。那是个错误。这是绝对必要的!如果省略这些,则搜索工作,但 SearchResult.basic 和 SearchResult.ItemJoin 只返回空值。
TransactionSearchAdvanced advanced = new TransactionSearchAdvanced()
columns = new TransactionSearchRow()
basic = new TransactionSearchRowBasic()
// new SearchColumnSelectField() is required!!!!!!
internalId = new SearchColumnSelectField[] new SearchColumnSelectField()
,
itemJoin = new ItemSearchRowBasic()
// new SearchColumnSelectField() is required!!!!!!
externalId = new SearchColumnSelectField[] new SearchColumnSelectField()
【讨论】:
以上是关于SuiteTalk 高级搜索示例的主要内容,如果未能解决你的问题,请参考以下文章