从Netsuite中的保存搜索获取字段值为空

时间:2014-09-10 06:05:42

标签: java web-services soap netsuite suitetalk

我在Netsuite中创建了一个已保存的搜索。我试图通过我的应用程序的Web服务调用获取字段值。我正确地获得了总记录大小,但在尝试获取字段值时,我从保存的搜索中获取所有字段(例如,itemname)的空值。这是代码:

TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
tsa.setSavedSearchId("825");
SearchResult result = _port.search(tsa);
System.out.println("size of recds..." + result.getTotalRecords());

if (result.getTotalRecords() > 0) {
    // retain the search ID in order to get more pages 
    String sSearchId = result.getSearchId();
    SearchRowList rowList = result.getSearchRowList();
    processRowList(rowList);
    int iNumPages = result.getTotalPages();
    System.out.println("pages" + iNumPages);
    for (int i = 2; i <= iNumPages; i++) {
        result = _port.searchMoreWithId(sSearchId, i);
        rowList = result.getSearchRowList();
        System.out.println("jjjj" + rowList);
        processRowList(rowList);
    }
}

_port.logout();

public void processRowList(SearchRowList rowList) {
    try {
        System.out.println("inside process...." + rowList);
        for (int i = 0; i < rowList.getSearchRow().length; i++) {
            TransactionSearchRow ts = (TransactionSearchRow) rowList.getSearchRow(i);
            TransactionSearchRowBasic tsb = ts.getBasic();
            SearchColumnSelectField[] scsf = tsb.getItem();
            for (SearchColumnSelectField sf: scsf) {
                RecordRef rf = sf.getSearchValue();
                System.out.println("itemname:::" + rf.getName());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:1)

我是Netsuite的新手,但我非常渴望解决同样的问题,我想我有答案。我认为,当您尝试打印项目名称时,您试图迭代一行可能具有的所有可能字段。但是,这个&#34;项目&#34; TransactionSearchRowBasic包含的对象不是此对象可能包含的项列表。然后,您必须尝试获取事务保存搜索所包含的值。

所以,让我们说我有一个交易保存搜索,我想在其中显示交易的期间,类型,帐户,金额,日期和相关部门的外部ID。首先,我在此链接中查找了每个记录的API ID与其UI名称相比:

https://system.netsuite.com/help/helpcenter/en_US/RecordsBrowser/2012_2/Records/transaction.html

然后我使用了一个非常类似的java代码,但有一些修复。

此方法验证一切正常并调用真正打印数据字段的方法:

public void printTransactionRowFields() throws ExceededUsageLimitFault, UnexpectedErrorFault, InvalidSessionFault, ExceededRecordCountFault, RemoteException, UnsupportedEncodingException, SOAPException
    {
        TransactionSearchAdvanced tsa = new TransactionSearchAdvanced();
        //This is my Transaction saved search Internal ID
        String savedSearchID = "999";
        tsa.setSavedSearchId(savedSearchID);

        SearchResult srr = _port.search(tsa);
        boolean isSuccess = srr.getStatus().isIsSuccess();
        System.out.println("Search result: "+ isSuccess);
        int searchNumRecords = srr.getTotalRecords();
        System.out.println("Amount of records: "+ searchNumRecords);
        if (isSuccess) 
        {
            if(searchNumRecords > 0)
            {
                int iNumPages = srr.getTotalPages();
                //Must check the amount of pages to process every record
                System.out.println("Num pages: "+ iNumPages);
                //Must retain search ID to search for more pages. This is a web service ID, it's not the same as the internal ID "999"
                String sSearchID = srr.getSearchId();
                System.out.println("Search id: "  + sSearchID);
                SearchRowList rowList = srr.getSearchRowList();
                System.out.println("Num rows in row list 1: " +rowList.getSearchRow().length);
                processReportRowList(rowList);
                //We process the next iNumPages pages (it's not 0 index)
                for ( int i=2; i<=iNumPages; i++) {
                    //Users who authenticate to NetSuite by providing their credentials in the SOAP header of
                    //their requests must use searchMoreWithId to retrieve search results that span multiple pages.
                    //They cannot use searchMore or searchNext, since both operations require an active session.
                    //(For information on request-level-credential authentication, see Request-Level Credentials.)
                    srr = _port.searchMoreWithId(sSearchID, i);
                    rowList = srr.getSearchRowList();
                    System.out.println("Num rows in row list #" +i+": " +rowList.getSearchRow().length);
                    processReportRowList(rowList);
                } 
            }else{
                System.out.println("This saved search contains no records to process.");
            }
        }
        else{
            System.out.println("Search could not find Transaction Saved Search with id: " + savedSearchID);
        }
    }

此方法处理我的交易期间,类型,帐户,金额,日期,部门名称字段的打印:

public void processReportRowList(SearchRowList rowList) 
    {
        if(rowList!= null){
            System.out.println("Search row length: " + rowList.getSearchRow().length);
            for (int i=0; i<rowList.getSearchRow().length; i++) {
                TransactionSearchRow row=(TransactionSearchRow)rowList.getSearchRow(i); 
                TransactionSearchRowBasic basic= row.getBasic();
                System.out.println((basic==null)?"Basic is Null.":"Basic is not null.");
                RecordRef period = basic.getPostingPeriod(0).getSearchValue();
                String periodInternal = period.getInternalId();
                String  type = basic.getType(0).getSearchValue();
                RecordRef account = basic.getAccount(0).getSearchValue();
                Double amount = basic.getAmount(0).getSearchValue();
                Double amountFC = basic.getFxAmount(0).getSearchValue();
                Calendar tranDate = basic.getTranDate(0).getSearchValue();
                DepartmentSearchRowBasic deparmentRow = row.getDepartmentJoin();
                String departmentExternalID = deparmentRow.getExternalId(0).getSearchValue().getInternalId();
                //Print results
                System.out.println("Row# "+i+"\n"+"Period Internal ID: "+periodInternal +"\n Type: "+ type+ "\n Account Internal ID: "+
                accountInternal + "\n Amount: "+ amount + "Foreign Currency Amount: "+ amountFC + "\n Date: " + tranDate.getTime().toString()+
                "\n Department External ID: "+departmentExternalID);
            }

        }else{
            System.out.println("Row list arrived null");
        }
    }

请注意,我必须创建一个DepartmentSearchRowBasic,因为我试图从Department记录中获取部门外部ID。在我保存的搜索中,我使用&#34;部门字段...&#34;选择了部门外部ID。选项。

如果您迷失在TransactionSearchRowBasic对象上的哪个方法将返回您在保存的搜索结果中选择的正确字段,您可以使用Eclipse的调试模式来查看哪些字段不为空以及它们返回的类型并包含。

希望这个答案可以帮助所有人因缺乏优秀的Netsuite文档而绝望! (虽然,这只是我的意见)

答案 1 :(得分:0)

以下是执行搜索并循环显示结果的示例,并将子公司名称(Record = Subsidiary,Field Name = name)分配给变量。每次出现循环时都会重新赋值变量值,仅供参考。

认为您正在寻找 getFieldValue 功能。

提示:使用NetSuite Records Browser&amp;模式浏览器,用于记录和列的有效列表。

var rec = '';
var subsidiaryName = '';
var searchResults = nlapiSearchRecord( 'subsidiary', null, null, null );
for ( var i = 0; i < Math.min( 100, searchResults.length ); i++)
{
    var record = nlapiLoadRecord(searchResults[i].getRecordType(),searchResults[i].getId() );
    subsidiaryName = record.getFieldValue('name');
}