我正在尝试创建一个允许我们搜索Netsuite记录的C#应用程序(使用suitetalk)。记录类型将动态指定。你能帮忙吗?
我检查了网络服务,并确定SearchRecord类有许多类型为AccountSearch,ItemSearch等的子类。
但是,我想动态地进行这些搜索。
AccountSearch acc = new AccountSearch();
SearchResult searchresult = new SearchResult();
searchresult = _service.search(acc);
上面的代码为我提供了帐户列表。但是,AccountSearch在这里是硬编码的。
答案 0 :(得分:0)
下面的代码可以使用。
SearchRecord search;
SearchRecord searchCriteria;
SearchRecordBasic searchBasicCriteria;
if(recordType.equals(RecordType.account)){
search = new AccountSearchAdvanced();
searchCriteria = new AccountSearch();
searchBasicCriteria = new AccountSearchBasic();
//set criteria on searchBasicCriteria
((AccountSearch) searchCriteria).setBasic((AccountSearchBasic) searchBasicCriteria);
((AccountSearchAdvanced) search).setCriteria((AccountSearch) searchCriteria);
}else if(recordType.equals(RecordType.customer)){
search = new CustomerSearchAdvanced();
searchCriteria = new CustomerSearch();
searchBasicCriteria = new CustomerSearchBasic();
//set criteria on searchBasicCriteria
((CustomerSearch) searchCriteria).setBasic((CustomerSearchBasic) searchBasicCriteria);
((CustomerSearchAdvanced) search).setCriteria((CustomerSearch) searchCriteria);
}else{
search = null;
}
if(search != null) _service.search(search);
但我认为更好的解决方案是为每种类型的搜索创建特定的方法。这样代码更具可读性,而且你可以避免所有的转换。然后,您必须为每个特定记录类型处理返回的RecordList。